简体   繁体   English

在Phalcon中使用带名称空间的相关模型

[英]Using Related Models in Phalcon with Namespaces

I've been working with Phalcon and slowly getting to grips with it. 我一直在与Phalcon合作,并逐渐掌握它。 However, I've stumbled across an issues and it's got me stumped. 但是,我偶然发现了一个问题,这让我很困惑。 I'm hoping that someone else can provide some assistance. 我希望其他人可以提供一些帮助。

I have two tables in the DB that are related as a one-to-many. 我在数据库中有两个表,它们是一对多的。 clients->sites. clients->网站。 These are the two definitions of the models in Phalcon: 这是Phalcon中模型的两个定义:

#File: CrmClients.php
namespace CRM\Models;
use Phalcon\Mvc\Model\Resultset\Simple as Resultset;

class CrmClients extends \Phalcon\Mvc\Model
{
    public id;

    public function initialize()
    {
        $this->hasMany("id", "CRM\Models\CrmSites", "client_id", array("alias" => "Sites"));
    }
}

#File: CrmSites.php
namespace CRM\Models;

class CrmSites extends \Phalcon\Mvc\Model
{
    public id;
    public client_id;

    public function initialize()
    {
        $this->belongsTo("client_id", "CRM\Models\CrmClients", "id", array("foreignKey" => true, "alias" => "Clients"));
    }
}

Then in the controller I have: 然后在控制器中,我有:

$profile = Clients::findFirstById($id);
$sites = $profile->Sites;

When I run this I end up with the following error: 运行此命令时,出现以下错误:

Notice: Access to undefined property CRM\\Models\\CrmClients::Sites in \\html\\apps\\crm\\controllers\\ClientsController.php on line 51 注意:访问第51行上\\ html \\ apps \\ crm \\ controllers \\ ClientsController.php中未定义的属性CRM \\ Models \\ CrmClients :: Sites

I'm at a loss as to what I'm doing wrong here, and any help would be greatly appreciated. 对于我在这里做错的事情我感到茫然,任何帮助将不胜感激。

If you have any questions, or need any clarification, please just ask. 如果您有任何疑问或需要澄清,请问。

Thanks for your help in advance. 感谢您的帮助。

After much reviewing and some help from the Phalcon forums also. 经过大量审查并从Phalcon论坛获得了一些帮助。 It transpired that there's nothing wrong with the code. 它发觉到代码没有错。

The problem came down to a "user error" in that I had two copies of the CrmClients.php file in two separate modules. 问题归结为“用户错误”,因为我在两个单独的模块中拥有CrmClients.php文件的两个副本。 I was editing the CrmClients in the wrong folder, and as such the CrmClients.php file with the CrmSites.php didn't have an initialize function at all - probably why it wasn't finding the relationship... 我在错误的文件夹中编辑了CrmClients,因此带有CrmSites.php的CrmClients.php文件根本没有初始化函数-可能是为什么找不到关系...

As such, I added the initialize function and it's working perfectly. 这样,我添加了initialize函数,它运行良好。

@digitronic: really appreciate the help on this one. @digitronic:非常感谢您对此提供的帮助。

Thanks again. 再次感谢。

You forgot third param in your hasMany() method in CrmClients model. 您在CrmClients模型的hasMany()方法中忘记了第三个参数。

you code looks like: 您的代码如下所示:

$this->hasMany("id", "CRM\Models\CrmSites", array("alias" => "Sites"));

and it should be like: 它应该像这样:

$this->hasMany("id", "CRM\Models\CrmSites", 'client_id', array("alias" => "Sites"));

seems to me that could be related to error that you are getting ... 在我看来,这可能与您得到的错误有关...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM