i would like to paginate a list of games, where the sport is a chosen sport.
the relatition is as followed:
Game BelongsTo Competition BelongsTo Team BelongsTo sport
What i would like to do is show all games where the teams sport_id = 1.
The following doesn't work:
$this->paginate = array('limit' => 30, 'page' => 1,
'conditions' => array('Competition.Team.sport_id' => '1'),
'contain' => array('Competition', 'Competition.Team',
'Gamefield', 'Changingroom', 'ChangingroomAway', 'Gametype'),
'order'=>array('game_date'=>'asc'),
);
Can anyone help me with this one?
I've found my solution:
var $paginate=array(
'Game'=>
array(
'joins'=>array(
array('table'=>'competitions',
'alias'=>'Competition2',
'type'=>'left',
'conditions'=>array('Game.competition_id=Competition2.competition_id')
),
array('table'=>'teams',
'alias'=>'Team2',
'type'=>'left',
'conditions'=>array('Competition2.team_id=Team2.team_id')
),
),
'order'=>array('game_date'=>'asc'),
'contains'=>array('Competition2'=>array('Team2'))
));
function index() {
$datum = date('Y-m-d H:m');
$this->Game->recursive = 0;
$scope=array('OR' => array(array('Team2.sport_id' => 2), array('Team2.sport_id' =>3)), 'Game.game_date >' => $datum);
// Configure::write('debug',2);
$this->set('games', $this->paginate(null,$scope));
}
Thanks to TehThreag for helping me
Containable doesn't create any joins unless the relation would have been joined anyways, despite using Containable.
This means that for habtm, if you want a join you have to do as Michael did and specify them.
Also, doing a couple of joins should be faster with logical indexes than doing a habtm with Containable anyways, as the results from a contain for the same data as above would require an in ( id1,id2,...id# ) condition and a number of queries from there to fetch the individual related records.
The joins solution gets the data back in one db query.
Related
I have three Mysql tables:
Brands ( brand names , brand Ids)
cars ( car names,car ids, brand Ids)
statuss ( car Ids, each car status).
I am trying to pull the brand names from the brand table but I have the cars IDs. How can I do that see my code below.
Controller.ctp
$this->loadModel('car');
$cars = $this->car>find('all',array('limit' => 6, 'order' => array('car.id' => 'asc')));
$this->set('cars', $cars);
$this->loadModel('status');
$cars = $this->status>find('all',array('limit' => 6, 'order' => array('status.id' => 'asc')));
$this->set('statuss', $statuss);
View.ctp
App::import('Controller', 'cars');
$carsCont = new carsController;
App::import('Controller', 'brands');
$brandCont = new brandsController;
foreach($statuss as $status)
{
$car_info = $carsCont->get_car_info($status['status']['car_id']);
$car_name = $car_info['car_name'];
$car_id = $status['status']['car_id'];
$brand_list = $brandCont -> get_brand_name($status['car']['brand_id']); <---- this is not working
echo $brand_list['brand']['brand']; echo $car_name;
}
CakePHP controllers are not designed or meant to be instantiated directly like you are doing.
Rather, you should be making use of the associations that cakephp gives you.
Based on what you've said, you've got Statuses belongsTo Cars belongsTo Brands. Set those relations up in your model files (as documented in the book: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html)
If you then use the containable behavior (http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html) you'd be able to do something like (In your status controller)
$statuses = $this->Statuses->find('all', [
'contain' => ['Cars' => 'Brands']
]);
and you should have all the data that you need.
Edit: Based on your additions, change your cars query to
$cars = $this->car>find('all',array('limit' => 6, 'order' => array('car.id' => 'asc'), 'contain' => ['Brands']));
But, make sure Cars BelongsTo Brands, and you've attached the Containable behaviour.
I can't display Agreements with agreement_number less than 7 and order it by agreement_number DESC.
I have read Pagination CakePHP Cook Book and can't find where my code is wrong. It display only less than 7, but always ASC. I have found similar question here, [that works],(CakePHP paginate and order by) and do not know why. Agreement.agreement_number is int(4).
$this->Agreement->recursive = 0;
$agreements = $this->Paginator->paginate('Agreement', array(
'Agreement.agreement_number <' => '7'
), array(
'Agreement.agreement_number' => 'desc'
)
);
$this->set('agreements', $agreements);
}
Exact cake version is 2.5.2.
... Where did you read that that was the correct syntax?
The paginate function's third parameter is for sorting (and I mean, within the table... with those down and up arrows).
List of allowed fields for ordering. This allows you to prevent
ordering on non-indexed, or undesirable columns.
You have the exact link used for documentation of the API, but you don't seem to be following it (like, from here and here)
$this->Paginator->settings = array(
'Agreement' => array(
'order' => array('Agreement.agreement_number' => 'desc')
)
);
$agreements = $this->Paginator->paginate('Agreement', array(
'Agreement.agreement_number <' => '7'));
I have a table called items and a table called item_pics.
item_pics has an item_id, file_name and a rank field (among others).
What I'm looking for is for each item my index page's $items array to contain the file_name from the item_pics matching the item's item_id with the lowest rank. So I can access like (or something like) this in my Items/index.ctp:
foreach ($items as $item):
$img = $item['Item']['ItemPic']['file_name'];
...
I'm pretty new to CakePHP, this is my first project. I thought that this within the Item model would cause item_pics data to be pulled (although I figured all related item_pics for each item would get pulled rather than just the one with the lowest rank):
public $hasMany = array(
'ItemPic' => array(
'className' => 'ItemPic',
'foreignKey' => 'item_id',
'dependent' => false
)
}
but I can see that no item_pics data is loaded (at the bottom of items/index):
SELECT `Item`.`id`, `Item`.`title`, `Item`.`description`, `Item`.`created`, `Item`.`modified`, `Item`.`type`, `Project`.`id`, `Project`.`item_id`, `Project`.`title`, `Project`.`description`, `Project`.`rank`, `Project`.`created`, `Project`.`modified`
FROM `laurensabc`.`items` AS `Item`
LEFT JOIN `laurensabc`.`projects`
AS `Project`
ON (`Project`.`item_id` = `Item`.`id`)
WHERE `Item`.`type` IN (1, 2)
LIMIT 20
also, while I would like projects to be joined in the view pages, I don't really need them in the index page.
I've done some searching and haven't been able to find exactly what I'm looking for. I suppose I could do a query within the index view item loop, but I'm trying to make sure I do things the right way... the CakePHP way. I assume I need to change something about my model relationships but I haven't had any luck.
CakePHP - Associations - HasMany, this makes it seem like I could order by rank and limit 1. But this didn't work... and even if it did, I wouldn't want that to affect the view pages but rather just the index page.
My Controller looks like this:
public function index($type = null) {
$this->Item->recursive = 0;
$conditions = array();
if ($type == "sale") {
$conditions = array(
"Item.type" => array(self::FOR_SALE, self::FOR_SALE_OR_RENT)
);
} else if ($type == "rent" ) {
$conditions = array(
"Item.type" => array(self::FOR_RENT, self::FOR_SALE_OR_RENT)
);
} else {
$conditions = array("Item.type !=" => self::HIDDEN);
}
$paginated = $this->Paginator->paginate($conditions);
debug($paginated);
$this->set('items', $paginated);
$this->set('title', ($type == null ? "Items for Sale or Rent" : "Items for " . ucwords($type)));
}
I have also tried this on my controller, but it doesn't seem to do anything either:
$this->paginate = array(
'conditions' => $conditions,
'joins' => array(
array(
'alias' => 'ItemPic',
'table' => 'item_pics',
'type' => 'left',
'conditions' => array('ItemPic.item_id' => 'Item.id'),
'order' => array('ItemPic.rank' => 'asc'),
'limit' => 1
)
)
);
$paginated = $this->paginate($this->Item);
First, set containable behavior in AppModel (or if you don't want it on each model, put it on Item model):
public $actsAs = array('Containable');
Then, on your find query:
$items = $this->Item->find('all', array(
'contain' => array(
'ItemPic' => array(
'fields' => array('file_name'),
'order' => 'rank',
'limit' => 1
)
)
));
Then the result array you can access it like:
foreach ($items as $item):
$img = $item['ItemPic']['file_name'];
Edit: Then you should put it on the paginate query:
$this->paginate = array(
'conditions' => $conditions,
'contain' => array(
'ItemPic' => array(
'fields' => array('file_name'),
'order' => 'rank',
'limit' => 1
)
)
);
In this case, I would probably order by rank and limit 1 as you said, and make that a dynamic association just for the index page (See http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly). So use $this->Item->bindModel(array('hasMany' => array('ItemPic' => $options))); (which I believe should replace your current settings for HasMany ItemPic, but you may have to unbindmodel first)
Associations created through bindModel will go through for the next query only, then it'll revert to your normal settings, unless you specifically set an option to keep using the new association.
As for why it's not getting ItemPics with Items, or why trying to order by rank and limit 1 didn't work for you, I can't really say without seeing more of your code.
I have a three tables which have associations as follows:
listings hasmany bookings
listings hasmany special_prices
Tables:
bookings.id, bookings.start, bookings.stop, bookings.listing_id
special_prices.id, special_prices.start, special_prices.stop, special_prices.price, special_prices.listing_id,
And of course the Listings table.
The associations are all set up in the Model file courtesy of cake bake. Users are able to search all the listings according to area, a start date and a finish date. I'm trying get a set of results where only listings are returned if they don't have a start date which is BETWEEN the dates of any booking that listing might have.
In my search function I have:
$conditions = array(
"AND" => array(
"OR" => array(
"Listing.address_one LIKE" => "%".$area."%",
"Listing.address_two LIKE" => "%".$area."%",
"Listing.city LIKE" => "%".$area."%",
"Listing.postcode LIKE" => "%".$area."%",
"Listing.title LIKE" => "%".$area."%",
),
)
);
$this->Listing->bindModel(array('hasMany' => array('Booking' => array('conditions' =>array('Booking.checkin BETWEEN ? AND ?' => array($to, $from))))));
$data = $this->paginate('Listing', $conditions);
$this->set('data', $data);
I read here: http://ibndawood.com/2011/10/how-to-filter-hasmany-related-model-records-in-cakephp-when-using-find-function/ that the other option is to use
$this->Listing->hasMany['Booking']['conditions'] = array('Booking.checkin BETWEEN ? AND ?' => array($to, $from));
But unfortunately both these just return all the entries in the database which match the area but completely ignores the Bookings table it seems. I've tried making the condition
'Booking.id' => '5'
To test, but it ignores this too.
Can anyone shed some light on this for me. There's not too many examples in the documentation and what's there ain't working for me.
EDIT
I've managed to find out that because cakephp doesn't automatically join hasmany relationships when you are paginating, therefore I had to add some code to tell it to join the bookings table.
Now it seems to be joining alright with the Bookings table but it's returning multiple sets of Listings. Can anyone take a look and help me out with this? Here's what I have now:
$conditions = array(
"OR" => array(
"Listing.address_one LIKE" => "%".$area."%",
"Listing.address_two LIKE" => "%".$area."%",
"Listing.city LIKE" => "%".$area."%",
"Listing.postcode LIKE" => "%".$area."%",
"Listing.title LIKE" => "%".$area."%",
)
);
$this->paginate = array(
'joins'=>array(
array(
'table'=>'bookings',
'alias'=>'Booking',
'type' =>'inner',
'conditions' =>array('Booking.checkin NOT BETWEEN ? AND ?' => array($to, $from))
)
),
'conditions'=> $conditions
);
$data = $this->paginate();
Booking is already bound to Listing in the models right? then there is no need to use bindModel on the fly.
just add your Booking.checkin BETWEEN $to AND $from into the $conditions array
I've got a series of Post models that hasAndBelongsToMany Media models. In certain function calls inside of the Post model, I don't need to retrieve the entire list of Media models. However, when I use the following code:
$this->unbindModel( array('hasAndBelongsToMany' => array('Media')) );
// Rebind to get only the fields we need:
$this->bindModel(
array('hasAndBelongsToMany' => array(
'Media' => array(
'className' => 'Media',
'joinTable' => 'media_posts',
'foreignKey' => 'post_id',
'associationForeignKey' => 'media_id',
'limit' => 1,
'fields' => array('Media.type', 'Media.path', 'Media.title')
)
)
)
);
$this->find('all', $params);
This limit only works on one of the first retrieved Post model and all following Post models have no associated Media:
Array
(
[0] => Array
(
[Profile] => Array
(
)
[Media] => Array
(
[0] => Array
(
[type] => photo
[path] => ''
[title] => ''
)
)
)
[1] => Array
(
[Profile] => Array
(
)
[Media] => Array
(
)
)
)
Any suggestions would be great. Thanks!
why not use the containable behaviour
// you would probably want the next line in the app_model ot be able to use it with all models
$this->Post->actsAs = array('Containable')
$params['conditions'] = array(
);
$params['contain'] = array(
'Media' => array(
'fields' => array(
'type', 'path', 'title'
),
'limit' => 1
)
);
$this->Post->find('all', $params);
EDIT:
Just tried that and got this sql (Module <-> Tag):
SELECT `Module`.`id` FROM `modules` AS `Module` WHERE 1 = 1
and
SELECT `Tag`.`id`, `ModulesTag`.`module_id`, `ModulesTag`.`tag_id`
FROM `tags` AS `Tag`
JOIN `modules_tags` AS `ModulesTag`
ON (`ModulesTag`.`module_id` IN (1, 2, 3, 4) AND `ModulesTag`.`tag_id` = `Tag`.`id`)
WHERE `Tag`.`belongs_to` = 'Module'
ORDER BY `Tag`.`name` ASC
LIMIT 1
obviously that cannot return the wanted result, as you would have to do a query for each Module result (which then again would result in way too many queries).
As a conclusion I would return all Tags (in my example) as the overhead in too many result rows is better than the overhead of too many queries..
Cake fetches all the Habtm-related records in one batch query and then assembles them into the results array afterwards. Any additional conditions you specify in the association will be used as is in the query, so it'll look something like this:
SELECT … FROM Media WHERE Media.id in (1, 2, 3, …) LIMIT 1
So it'll only retrieve a single HABTM model.
There's no apparently easy solution for this. Maybe you could think about the original premise again and why the "first" (LIMIT 1) record is supposedly the right one, maybe you can find a different condition to query on.
Failing that, you could rebind your models so Media has a hasMany relationship to medias_posts, the pivot table. For hasMany and belongsTo queries, Cake automatically does JOIN queries. You could use a GROUP BY clause then, which would give you the desired result:
SELECT … FROM Media JOIN medias_posts … GROUP BY medias_posts.post_id
You might also want to experiment with passing the 'join' parameter with the query, to achieve that effect without extensive rebinding.
$this->Media->find('all', array('join' => array(…), …));
Try this:
$this->yourModel->hasAndBelongsToMany['Media'] = false; // or null
And then set your HABTM association manually
$this->yourModel->hasAndBelongsToMany['Media'] = array(........);
Or simply modify the association without nulling it:
$this->yourModel->HABTM['Media']['fields'] = array(....)
CakePHP has a very powerful tool for this containable behaviour