i'm using the searchplugin from cakeDC (https://github.com/CakeDC/search) with cakePHP 2.3.0. That Plugin works fine. I had a little error in the index-action like this. Thanks for that.
Indirect modification of overloaded property AtlasController::$paginate has no effect [APP\Controller\AtlasController.php, line 47]
My Index-action
public function index() {
$this->Prg->commonProcess();
$this->paginate['conditions'] = $this->Atla->parseCriteria($this->passedArgs);
$this->Atla->recursive = 0;
$this->set('atlas', $this->paginate());
$this->set('_serialize',array('atlas'));
}
The question is, how can i solve it? So i found out a way thats so simple and easy.
So this is the solution, that runs on my implementation.
I change the paginate()-call from.
$this->set('atlas', $this->paginate());
to the new paginate()-call.
this->set('atlas', $this->paginate($this->Atla->parseCriteria($this->passedArgs)));
Here the new index-action.
public function index() {
$this->Prg->commonProcess();
$this->Atla->recursive = 0;
$this->set('atlas', $this->paginate($this->Atla->parseCriteria($this->passedArgs)));
$this->set('_serialize',array('atlas'));
}
I believe the reason why the code did not work out of the box is the ['options'] key. Remove the key from $this->paginate['options'] and add the model as an argument for paginate in $this->set() and the pagination should work as expected. See the modified code example below.
public function index() {
$this->Prg->commonProcess();
$this->paginate = $this->Atla->parseCriteria($this->passedArgs);
$this->Atla->recursive = 0;
$this->set('atlas', $this->paginate('Atla'));
$this->set('_serialize',array('atlas'));
}
Related
For CakePHP2, I used following function for resetting associations.
public function unbindModelAll($reset = true) {
foreach(array('hasOne','hasMany','belongsTo','hasAndBelongsToMany') as $relation){
$this->unbindModel(array($relation => array_keys($this->$relation)), $reset);
}
}
How can I reset them for CakePHP3?
For CakePHP 2.x you can easily clear bindings using empty contain on the model
$this->Model->contain();
For CakePHP 3.x you can try, I didn't test it:
$this->Model->find()->contain();
And then another message mostly the same saying
Declaration of Cart::save() should be compatible with Model::save
The basic problem is that the cart refuses to update whenever I try to add a product to it(its a simple ajax script).
I wish I could talk more about this but I have no clue whats wrong. I have tried passing different parameters into the function but nothing works. Here are the relevant fucntions, sorry it being long.
CartsController:
$carts = $this->Cart->read();
$products = array();
if (null!=$carts) {
foreach ($carts as $productId => $count) {
$product = $this->Product->read(null,$productId);
$product['Product']['count'] = $count;
$products[]=$product;
}
}
$this->set(compact('products'));
}
public function update() {
if ($this->request->is('post')) {
if (!empty($this->request->data)) {
$cart = array();
foreach ($this->request->data['Cart']['count'] as $index=>$count) {
if ($count>0) {
$productId = $this->request->data['Cart']['product_id'][$index];
$cart[$productId] = $count;
}
}
$this->Cart->save($cart);
}
}
$this->redirect(array('action'=>'view'));
}
Cart Model:
public function save($data) {
return CakeSession::write('cart',$data);
}
/*
* read cart data from session
*/
public function read($data) {
return CakeSession::read('cart', $data);
}
Thanks for any help.
This kind of question has now been asked a felt million times on Stackoverflow. Here is the basic workflow of how to deal with compiler errors and notices that will for sure help you to solve them all on your own:
Read error message
Think about it for a moment
Google for the error message
The error tells you that your method signature must match the inherited parents signature. The arguments must match.
As mark already has mentioned in a comment, overloading the core class methods should only be done if you're pretty sure that you know what you're doing. if not this is very likely ending up in a pile of fail.
You can disable the strict errors but this is really not recommended, they exist for a reason. See How to eliminate php5 Strict standards errors?
Looks like you're doing a cart, well, here is my gift for the CakePHP ecommerce developers: https://github.com/burzum/cakephp-cart-plugin
In my AppModel, I have a beforeSave function:
public function beforeSave($options = array()) {
if (!empty($this->data[$this->alias]['created'])) {
$this->data[$this->alias]['created'] = date('Y-m-d G:i:s');
$this->data[$this->alias]['modified'] = $this->data[$this->alias]['created'];
} else {
$this->data[$this->alias]['modified'] = date('Y-m-d G:i:s');
}
$this->data[$this->alias]['modified_by'] = AuthComponent::user('id');
return true;
}
It's a simple function that just records the created and modified dates, and sets a field modified_by to the id of the currently logged in user. Or at least that's what it's supposed to do, everything works except getting the id of the currently logged in user, and I can't figure out why, because as far as I can tell in the documentation, that's exactly how it should be called.
I figured out my issue, and I feel a little dumb because of it, but thought I would add it here in case anyone else runs into the same problem.
In my News model, I was also calling the beforeSave function as well, but I forgot a line in it, namely parent::beforeSave();. How the date stuff in the beforeSave function was working, I don't know, but this fixed my problem of part of the beforeSave function not working.
Thanks for everyone's help in this matter, it's appreciated.
why not grab your user id from
CakeSession::read( 'Auth.User.Id ) ?
I think you should set user id in AppControlle::beforeFiler() method instead of AppModel
$this->data[$this->modelClass]['modified_by'] = $this->Auth->User('id');
I want to list the posts of a given user. It work but paginate is not accurate.
My code is the following
public function index($userid = null) {
if ($this->Post->exists($userid)) {
$this->set('posts',$this->Post->find('all',array('conditions'=>array('user_id'=>$userid))),
$this->paginate());
} else
{
$this->Post->recursive = 0;
$this->set('posts', $this->paginate());
}
The result give the correct list --> 3 posts, but the paginator display page number 1 and 2
Can you help me?
Thank you
Refer to the documentation
The code in the question is quite confused.
find
The find method only has two parameters:
find(string $type = 'first', array $params = array())
The third parameter (the result of calling paginate) isn't used and will be ignored - but it will setup the view variables for the pagination helper, based on the conditions used in the paginate call - there are no conditions being used.
It is not possible to paginate the result of a find call - to do so restructure the code to call paginate instead of find.
paginate
The paginate method is just a proxy for the paginator component - it can be used in several ways, this one (controller code example):
$this->paginate($conditions)
Is the most appropriate usage for the case in the question i.e. the complete action code should be similar to:
public function index($userId = null) {
$conditions = array();
if ($userId) {
$conditions['Post.user_id'] = $userId;
}
$this->set('posts',$this->paginate($conditions));
}
Note that logically, if a user id is requested that doesn't exist the response should be nothing - not everything.
I'm quite sure that conditions for paginate do now work that way.
If you want to set conditions for paginations you should do it as follows:
$this->paginate = array('conditions' => array('Post.user_id' => $userid)));
$this->set('posts', $this->paginate());
And yes, the result stored in $posts ( in view ) will be proper as you assigned proper find result to it, meanwhile you've paginated post model without any conditions whatsoever.
First off, you're checking to see if the post exists but using the $userid. Are you trying to see "if the user exists, get the posts for that user, or else get posts for ALL users"? As you have it right now, say you have the $userid = 159, but the max Post.id in your database is 28, then the condition is not being met because it is checking to see whether or not there is a Post with the id = 159 that exists, which it doesn't.
Second, your conditions are wrong. You are performing a find and then a paginate which are two separate queries. The conditions are being implemented on the find query but not the paginate but you are only displaying the find results.
public function index($userid = null) {
// setting recursive outside of if statement makes it applicable either way
$this->Post->recursive = 0;
// check if user exists
if ($this->Post->User->exists($userid)) {
// get posts for user
$this->set('posts', $this->paginate('Post', array('Post.user_id' => $userid));
}
else{
// get all posts
$this->set('posts', $this->paginate('Post'));
}
} // end index function
Please help, this is my first plugin I'm writing and I'm completely lost. I'm trying to write and update information in a table in a joomla database using my custom giveBadge() function. The functions receives two different variables, the first variable is the $userID and the second one is the digit 300 which I pass at the bottom of the class using giveBadge(300). At the same comparing the $userID in the Joomla database to ensure that the number 300 is given to the current user logged in the Joomla site.
Thanks in advance.
<?php
defined('JPATH_BASE') or die;
class plgUserBadge extends JPlugin
{
public function onUserLogin () {
$user =& JFactory::getUser();
$userID =& user->userID;
return $userID;
}
public function giveBadge ($userID, &$badgeID) {
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Fields to update.
$fields = array(
'profile_value=\'Updating custom message for user 1001.\'',
'ordering=2');
// Conditions for which records should be updated.
$conditions = array(
'user_id='.$userID,
'profile_key=\'custom.message\'');
$query->update($db->quoteName('#__user_badges'))->set($fields)->where($conditions);
$db->setQuery($query);
try {
$result = $db->query();
} catch (Exception $e) {
// Catch the error.
}es = array(1001, $db->quote('custom.message'), $db->quote('Inserting a record using insert()'), 1);
}
}
giveBadge(300); //attaches to $badgeID
?>
Here is not going well with your code:
You can drop the assign by reference in all your code (&) - you really don't need it, in 99% of the cases.
Use an IDE (for example Eclipse with PDT). At the top of your code you have & user->userID; Any IDE will spot your error and also other things in your code.
Study existing plugins to understand how they work. Here is also the documentation on plugins.
The method onUserLogin() will automatically be called by Joomla when the specific event is triggered (when your plugin is activated). Check with a die("My plugin was called") to see if your plugin is really called
inside onUserLogin() you do all your business logic. You are not supposed to return something, just return true. Right now your method does absolutely nothing. But you can call $this->giveBadge() to move the logic to another method.