duanoucuo7045 2012-12-13 21:28
浏览 127
已采纳

模型与自定义表和键名的关联

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

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.

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

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

Here are my models:

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);

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) works just fine. It shows me the fields from the contacts table.

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?

  • 写回答

1条回答 默认 最新

  • duanhuancong1969 2012-12-14 07:54
    关注

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

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

    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')
    );
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值