【问题标题】:hasMany relationship with conditionshasMany与条件的关系
【发布时间】:2015-04-11 13:40:08
【问题描述】:

这个主题可能是一个常见问题,但我有一个更深层次的问题。我是 CakePhp 的大一新生,已经用谷歌搜索了这个问题。不幸的是,我找不到确切的解决方案。

我的数据库结构如下,有两个表,我使用 cakePhp 命名约定:

- news (id, news_title, news_text, is_active, created, modified)

- news_images (id, news_id, image_src, is_default, created, modified)

每条新闻都有很多图片。但是默认选择了一张图片 用作首页的缩略图。

在新闻列表页面中,我想用缩略图列出所有新闻。 缩略图表示,news_images.is_default=true

所以我必须建立一个 hasMany 关系,但使用 is_default=true 过滤

如果我只是在 hasMany 和 belongsTo 关系之后获取数据而不使用任何条件,它会检索所有图像。当我对 cakePhp 太陌生时,我无法成功 bingModal 或容器。

我想请求您的帮助。 提前致谢。

【问题讨论】:

  • Cakephp 是什么版本的?
  • 您的关联可能不正确,但如果没有看到关联或您尝试执行的查找(基本上是任何相关代码),将很难提供帮助。
  • @AgRizzo CakePHP 2.6.3 关联是这样的:新闻模型(News.php)class News extends AppModel { public $hasMany = array('news_image' => array( 'conditions' => array('news_image.is_default' => true) ) ); }和NewsImage.phpclass NewsImage extends AppModel{ public $belongsTo = array('News'); }谢谢
  • @DoNhuVy 感谢您的提议。如您所述,我将编辑我的数据库结构。但是我应该如何在我的模型中设置我的关联?我有两个模型,News 和 NewsImage。新闻:public $hasMany = array('news_image'); NewsImage:public $belongsTo = array('News'); 或者新闻应该是 $hasOne?作为大一新生,我不能决定吗?再次感谢。

标签: cakephp has-many belongs-to


【解决方案1】:

有两种主要方法可以实现您想要的。

A:查找包含条件

$data = $this->News->find('all', array(
    'contain' => array(
        'NewsImage' => array(
            'conditions' => array(
                'is_default' => true
            )
        )
    )
));

B:将第二个关联添加到带有条件的新闻模型

如果您要在多个地方执行此查找调用,这是更好的方法。

在模型中新闻(news.php):

public $hasMany = array(
    'Image' => array(
        'className' => 'Image',
        'foreignKey' => 'news_id',
        'dependent' => false,
    ),
    'NewsThumbnail' => array(
        'className' => 'NewsImage',
        'foreignKey' => 'news_id',
        'conditions' => array('is_default' => true),
        'dependent' => false,
    )
);

现在您可以找到包含 NewsThumbnail 的新闻,并且会自动应用条件。

$data = $this->News->find('all', array(
    'contain' => array(
        'NewsThumbnail'
    )
));

注意:如果您在此处的包含中添加更多条件,它们将覆盖模型关联中设置的条件,因此您必须再次包含它们。

【讨论】:

  • 谢谢。我正在用你的完美技巧继续学习 CakePhp。我用@DoNhuVy 的提议重新编辑了我的数据库结构。
【解决方案2】:

数据库:

(在表中imagesnews_id是外键)

模特新闻 (news.php):

public $hasMany = array(
    'Image' => array(
        'className' => 'Image',
        'foreignKey' => 'news_id',
        'dependent' => false,
    )
);


模型图片 (image.php):

public $belongsTo = array(
    'News' => array(
        'className' => 'News',
        'foreignKey' => 'news_id',
    )
);

【讨论】:

    【解决方案3】:

    谢谢你。我正在用你完美的技巧在 CakePhp 中提高自己。尝试结合提示。

    1) 由@DoNhuVy 提供的重组数据库:
    news: id, title.., news_image_id
    news_images: id, src ..,news_id

    2) 然后使用你的模型如下@corie-slate

    class News extends AppModel {
     public $hasMany = array(
      'Image' => array(
       'className' => 'Image',
       'foreignKey' => 'entry_id',
       'dependent' => false),
      'NewsThumbnail' => array(
       'className' => 'NewsImage',
       'foreignKey' => 'entry_id',
       'dependent' => false));
    }
    

    到这里为止,一切都好吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-03
      • 2014-12-30
      • 2013-09-02
      • 2021-09-24
      • 1970-01-01
      • 2020-07-04
      • 2019-02-26
      • 2016-06-13
      相关资源
      最近更新 更多