Hello Community,
I have a problem with CPagination not giving me enough models.
There are two echo calls in my controller. If $_GET["ma"] is equal to 1, which is the id of the user i want to filter for,
the first echo outputs 41.
The second echo outputs 4.
I also set up a CLinkPager in my View. This shows me 3 pages. Each page with 4 models.
So there are 3*4=12 models.
I want 41 models to display there. 20 on first page. 20 on second page. 1 on third page.
I set up a CDbCriteria with CPagination within my Controller like this:
$criteria = new CDbCriteria();
if( isset($_GET["inland"]) && !empty($_GET["inland"]) ){
$criteria->addCondition("location LIKE :location");
$criteria->params[":location"] = "%".$_GET["location"]."%";
}
$criteria->with = array(
"users" => array(
"select" => false, // --- We don't want to load those related models
"together" => true, // --- Must include this so we can query based on the related players
"condition" => "1=1",
"params" => array(),
),
);
if( isset($_GET["ma"]) && !empty($_GET["ma"]) ){
$criteria->with["users"]["condition"] .= " AND user_id = :user_id";
$criteria->with["users"]["params"][":user_id"] = $_GET["ma"];
}
$count = Trip::model()->count($criteria);
echo $count;
$pages = new CPagination($count);
$pages->pageSize = 20;
$pages->applyLimit($criteria);
$trips = Trip::model()->findAll($criteria);
echo "<br>";
echo count($trips);
In my model I have the relation set up like this:
public function relations() {
return array(
'users' => array(self::MANY_MANY, 'User', 'trips_users(trip_id, user_id)'),
);
}
My Schema for the table "trips" look like this:
CREATE TABLE `trips` (
`trip_id` int(11) NOT NULL,
`location` varchar(255) DEFAULT NULL,
`inland` tinyint(1) NOT NULL DEFAULT '1',
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `trips`
ADD PRIMARY KEY (`trip_id`);
ALTER TABLE `trips`
MODIFY `trip_id` int(11) NOT NULL AUTO_INCREMENT;
And for table trips_users
CREATE TABLE `trips_users` (
`trip_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `trips_users`
ADD PRIMARY KEY (`trip_id`,`user_id`),
ADD KEY `user_id` (`user_id`);
ALTER TABLE `trips_users`
ADD CONSTRAINT `trips_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `trips_users_ibfk_2` FOREIGN KEY (`trip_id`) REFERENCES `trips` (`trip_id`) ON DELETE CASCADE ON UPDATE CASCADE;
Last table, "users", schema looks like this:
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`username` varchar(100) NOT NULL,
`department` varchar(255) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `users`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `username_index` (`username`);
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
Would be awesome if someone could point me into the right direction.
[b]EDIT:
If I turn this code into a comment:
if( isset($_GET["ma"]) && !empty($_GET["ma"]) ){ // if we want to filter a MA
$criteria->with["users"]["condition"] .= " AND user_id = :user_id";
$criteria->with["users"]["params"][":user_id"] = $_GET["ma"];
}
if( isset($_GET["department"]) && !empty($_GET["department"]) ){
$criteria->with["users"]["condition"] .= " AND department = :department";
$criteria->with["users"]["params"][":department"] = $_GET["department"];
}
Then I will not get enough models. So the problem is tracked down to this piece of code:
$criteria->with = array(
'users' => array(
'select' => false,
'together' => true,
"condition" => "1=1",
"params" => array(),
),
);
[/b]