尔康 - 延迟加载不是工作在模型的某些STANGE原因 [英] Phalcon - Lazy loading is not working in Models for some stange reason

查看:185
本文介绍了尔康 - 延迟加载不是工作在模型的某些STANGE原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的尔康和更多的Symfony的背景来了,我只需要知道如何在尔康模型定义外键之间可选的​​关系。

I am new to phalcon and coming from more of Symfony background, i just need to know how define optional relationship between foreign keys in phalcon models.

即 用户表的PK是USER_ID,而且用户可以有(可选)相关的记录表的照片有照片与主键photo_id和USER_ID链接。

i.e Users table PK is user_id, and Users May have(Optional) related record in Photos table to have photo with primary key photo_id and linked by 'user_id'.

因此​​,在我鹘模型我需要加载的用户与照片,如果有一个一。

So in my falcon models i need to load Users with the photos if there is a one.

在用户模式

 public function initialize()
{
    $this->hasMany(
        "photo_id",
        "Photo",
        "photo_id",
        array(
            'alias' => 'photos'
        )
    );
}

在照片模式,我把它写成

IN photo model i write it as

public function initialize()
{
    $this->belongTo(
        "user_id",
        "User",
        "user_id",
        array(
            'alias' => 'user'
        )
    );
}

在伏来访问它通过

{{user.photo.photo_id}}

但是,这给了我错误

But this gives me error

Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$photo_id

你知道吗?

奇怪的是,如果做到这一点code。在它的工作原理控制器,让我在照片模式而不是当我尝试用延迟加载访问/

Strangely if do this code in the controller it works and get me the photo models but not when i try to access it with lazy loading /

$users = User::find(array(
"limit" => 10)
);
echo "There are ", count($users), "\n";
foreach($users as $user){
$photos = Photo::find(array(
"user_id=:user_id:",
"limit" => 10,
"bind" => array("user_id" => $user->user_id)
)
);
if($photos->count()==0){
echo "<br />";
echo "No photo found for user".$user->user_id;
}
foreach($photos as $photo){
echo '<br />';
echo "User ->".$photo->user_id." Photo -> ".$photo->photo_id;
}
}

当我尝试用延迟加载这样做

When i try to do it with lazy loading like this

foreach($users as $user){
    //echo $user->photos->photo_id;
    foreach($user->photos as $photo){
        echo '<br />';
        echo "User ->".$photo->user_id."  Photo -> ".$photo->photo_id;
    }
 }

我得到的错误

i get error

未定义的属性:尔康\的mvc \型号\结果集\简单:: $ photo_id

Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$photo_id

推荐答案

问题似乎引起我有两个数据库连接如下图所示,这是造成在DI设定的一些问题。

Issue seems to be caused by i am having two database connections like below which is causing some problems in the DI set.

return new \Phalcon\Config(array(
    'database'    => array(
        'adapter'  => 'Mysql',
        'host'     => 'localhost',
        'username' => 'root',
        'password' => '',
        'dbname'   => 'db_main',
        'name'   => 'db_main',
    ),

需要保持只有一个数据库名称为DBNAME;

needs to keep only one database with name "dbname";

这就是我所做的修正

这篇关于尔康 - 延迟加载不是工作在模型的某些STANGE原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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