duanhe1965 2014-08-18 02:37
浏览 70
已采纳

Phalcon:如何以1-n关系获得第一个模型

I have the following models

class Post extends \Phalcon\Mvc\Model {
    ...
    $this->hasMany('id', 'PostMedia', 'parent_id');
}

class PostMedia extends \Phalcon\Mvc\Model {
    ...
    $this->belongsTo('parent_id', 'Post', 'id');
}

To get all related entries I do the following

Post::find(1)->postMedia;

But how do I get just the first model? The following code results in a Phalcon\Mvc\Model\Resultset\Simple object, but I need the PostMedia object.

Post::find(1)->getPostMedia(['limit' => 1]);
  • 写回答

1条回答 默认 最新

  • drzrzzkh462254 2014-08-18 06:01
    关注

    There are getFirst(), getLast() and other useful methods in the Phalcon\Mvc\Model\Resultset\Simple class. Also there is another problem in your code: Post::find(1) returns resultset, not Post object, so the correct code to get first related object will look like this:

    $first_media = Post::findFirst(1)->postMedia->getFirst();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国