与自定义表和键名的模型关联 [英] Model association with custom table and key names

查看:160
本文介绍了与自定义表和键名的模型关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 php.activerecord ,并且我正在尝试链接两个表.

I'm using php.activerecord, and I am trying to link two tables.

我有contactscontactCompanyLinks.每个联系人可以在链接表中包含一个或多个行.表中的字段名称没有任何明智的约定,但我无法更改它,因为它会破坏使用同一表的另一个应用程序.

I have contacts and contactCompanyLinks. Each contact can contain one or more rows in the link table. The field names in the table don't follow any sensible convention, but I can't change it as it would break another app using the same table.

contacts的主键称为contactID,而contactCompanyLinks中的外键称为inspectorID.

The primary key of contacts is called contactID, and the foreign key in contactCompanyLinks is called inspectorID.

php.activerecord进行了太多的假设,我不知道如何获得将表链接在一起的方法.

php.activerecord makes way too many assumptions, and I can't figure out how to get it to link my tables together.

这是我的模特:

Contact.php:

<?php
class Contact extends ActiveRecord\Model {
    static $primary_key = 'contactID';

    static $has_many = array(
        array(
            'contactCompanyLinks',
            'class_name' => 'ContactCompanyLink',
            'foreign_key' => 'inspectorID'
        )
    );
}

ContactCompanyLink.php:

<?php
class ContactCompanyLink extends ActiveRecord\Model {
    static $table_name = 'contactCompanyLinks';

    static $belongs_to = array(
        array('contact')
    );
}

这看起来不错,但是当我尝试排成一行时,它不起作用.我做了以下事情:

This looks right, but when I tried to get a row, it's not working. I did the following:

<?php
var_dump(Contact::find(1234)->contactcompanylinks);

我在屏幕上打印的全部是NULL!如果我尝试使用其他属性,例如contactcompanylinkContactCompanyLinkContactCompanyLinks,则会收到未定义的属性"错误.

All I got printed to the screen was NULL! If I tried other properties like contactcompanylink or ContactCompanyLink or ContactCompanyLinks, I got an "undefined property" error.

var_dump(Contact::find(1234)可以正常工作.它显示了contacts表中的字段.

var_dump(Contact::find(1234) works just fine. It shows me the fields from the contacts table.

当我试图告诉php.activerecord如何将2个表链接在一起时,我该如何停止假设并倾听我的声音?

How do I tell php.activerecord to stop assuming things and listen to me when I try to tell it how to link 2 tables together?

推荐答案

未定义的属性错误可能来自您的ID. phpactiverecord假定,不强制所有属性均为小写.

The undefined property-error comes probably from your ID. phpactiverecord assumes, nay FORCES all properties to be lowercase.

这意味着所有字段都应称为小写.例如,您的密钥应为inspectoridcontactid.

This means that all the fields should be called lowercase. E.G., your keys should be inspectorid and contactid.

并非仅列是这种情况.类名(php类)显然应该是实际情况,表名也应该如此.

Not that this is the case for columns only. Class names (php classes) should obviously be the case they actually are, and so should table names.

我总是明确定义连接的所有元素,以避免该假设问题.这意味着两个连接对我来说都具有所有要素:

I always explicitly define ALL elements of a connection to avoid that assumption problem. This means both connection will have all elements for me:

static $belongs_to = array(
  array('somename',
        'foreign_key'=>'someid',
        'primary_key'=>'id', 
        'class_name'=>'Models\\NameSpace\\YourModelClassName')
);

,但has_many也需要相同的字段. belongs_to中的主键是OTHER表的ID,外键是此表中的键(当我说出key时,我的意思是列名).因为属于外键的是另一个表中的键,而主键是此表中的键.

but also the same fields are needed for the has_many. The primary in the belongs_to is the id of the OTHER table, and the foreign key is the key in this table (and when I say key, I mean the column name). For the belongs to the foreign key is the key in the other table, and the primary key the key in this table.

此外,请注意名称空间的双斜杠.

Also, mind the double slashes for the namespace.

这篇关于与自定义表和键名的模型关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆