duanditang2916 2013-05-27 12:33
浏览 34

找不到类$ has_many通过phpactiverecord

I have a many to many relationship between Profesional and Plan so I wrote the following code:

Profesional.php

class Profesional extends BaseModel {

    static $table_name = 'profesionales';
    static $primary_key = 'idprofesional';

    static $has_many = array(
        array(
            '_planes',
            'class_name' => 'Plan',
            'through' => '_profplanes'
        ),      
        array(
            '_profplanes',
            'class_name' => 'ProfPlan',
            'foreign_key' => 'profesional'
        )
    );
}

profplan.php

class ProfPlan extends BaseModel{

    static $table_name = 'profplanes';
    static $belongs_to = array(
        array(
            '_plan',
            'class_name' => 'Plan',
            'foreign_key' => 'plan'
        ),
        array(
            '_profesional',
            'class_name' => 'Profesional',
            'foreign_key' => 'profesional'
        )
    );

}

plan.php

class Plan extends BaseModel {

    static $table_name = 'planes';
    static $primary_key = 'idplan';
    static $has_many = array(
        array(
            '_profplanes',
            'class_name' => 'ProfPlan',
            'foreign_key' => 'plan'
        )
    );
}

But I get Class Profplane does not exist. Where is the error? What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douhe1864 2013-05-28 15:59
    关注

    Fix the class / table spellings! it'll be easy to fix early in your code and becomes a maintenance nightmare later on =P! "Plan->Plans' 'Profesional=>Professional', "Professionales->"Professionals".

    Honestly the through relationship code in phpactiverecord is annoying, fickle, and really needs to be looked at in general.

    You should show how you're actually calling the code, but I'm going to assume you're doing

    Professional::find(1)
    $professional->_plans, //Error is thrown here
    

    You need to set up your code so that ProfPlan->_plans(or _planes in your case), is definded, and not just ProfPlan->_plan. Phpactiverecord needs to explicitly know the relationship it's going to go through. Let me know if you need a more clear explanation.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作