ItGo.me Focus on IT Recommend

Home > cakephp - hasMany relationship with conditions

cakephp - hasMany relationship with conditions

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

The subject may be a common question but I have a bit more deeper problem. I am a freshman in CakePhp and already googled the issue. Unfortunately I could not find my exact solution.

My database structure is below with two tables and I used cakePhp naming conventions:

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

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

Each news has many images. But one image is being selected as default to-be used as a thumbnail in homepage.

In news listing pages, I want to list all news with a thumbnail. Thumbnail means, news_images.is_default=true

So I have to make a hasMany relationship but filtering with is_default=true

If I simply fetched data after hasMany and belongsTo relationship without using any conditions, it's retrieving all images. And I could not succeeded bingModal or container while I am too new to cakePhp.

I would like to request your help. Thanks in advance.

cakephp has-many belongs-to
|
  this question
asked Apr 11 '15 at 13:40 hrnsarac 5 7 2   What version of Cakephp is this? –  AgRizzo Apr 11 '15 at 14:35      likely your associations aren't correct, but without seeing the associations or the find you're trying to do (basically any of the pertinent code), it's going to be difficult to help. –  Dave Apr 11 '15 at 14:43 1   @harunsarac , I will prefer this solution for database: news (id, news_title, news_text, is_active, created, modified, news_images_id) news_images (id, news_id, image_src, created, modified) with news_images_id (follow CakePHP naming convention) is featured image (for thumbnail) –  Do Nhu Vy Apr 11 '15 at 14:45      @AgRizzo CakePHP 2.6.3 The associations are like that: News Model (News.php) class News extends AppModel { public $hasMany = array('news_image' => array( 'conditions' => array('news_image.is_default' => true) ) ); } And NewsImage.php class NewsImage extends AppModel{ public $belongsTo = array('News'); } Thanks –  hrnsarac Apr 11 '15 at 20:53      @DoNhuVy Thanks for your offer. I will edit my db structure as you mentioned. But how should I set my associations in my models? I have two models, News and NewsImage. News: public $hasMany = array('news_image'); NewsImage: public $belongsTo = array('News'); Or should News be $hasOne? I could not decided as a freshman? Thanks again. –  hrnsarac Apr 11 '15 at 21:01

 | 

3 Answers
3

---Accepted---Accepted---Accepted---

There are two main ways to accomplish what you want.

A: Find Contain With Conditions

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

B: Add Second Association to News model with Conditions

This is the better method if you're going to perform this find call in more than one place.

In model News (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,
    )
);

Now you can find news, containing NewsThumbnail, and the conditions will be automatically applied.

Recommend:has and belongs to many - Return JSON array of associated entity IDs in CakePHP 3.0 belongsToMany relationship

join table, in this case), what is the best way in CakePHP 3.0 to include an array of the IDs of Bars associated with each Foo in the JSON returned by the RequestHandler through the _serialize property Specifically, I would like to be able

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

Note: If you add further conditions in the contain here, they will override the conditions set up in the model association, so you'll have to include them again.


|
  this answer
answered Apr 12 '15 at 11:06 Corie Slate 496 6 14      Thanks for that. I am moving forward to learn CakePhp with your perfect tips. I re-edited my db structure with the offer of @DoNhuVy. –  hrnsarac Apr 12 '15 at 21:35

 | 

Database:

(in table images, news_id is foreign key)

Model News (news.php):

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


Model Image (image.php):

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

|
  this answer
edited Apr 12 '15 at 1:50 answered Apr 12 '15 at 1:42 Do Nhu Vy 8,085 4 37 67

 | 

Thanks for that. I am improving myself in CakePhp with your perfect tips. Tried to combine the tips.

1) Re-structured db by the offer of @DoNhuVy:
news: id, title.., news_image_id
news_images: id, src.., news_id

2) Then used your models as below @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));
}

Until here, is everything right?


|
  this answer
answered Apr 13 '15 at 14:57 hrnsarac 5 7

 | 

Recommend:has and belongs to many - CakePHP find HABTM

e Message belongsto User Message HABTM Tag Tag HABTM Message If a User is logged in he might want to see all Message tagged with something. $messages = $this->Message->find('all', array( 'conditions' => array("Message.user_id" => $this-

------splitte line----------------------------