I am setting up memcache setup with cakephp3. I added below in config/app.php
'Cache' => [
'default' => [
'className' => 'Cake\Cache\Engine\MemcachedEngine',
],
]
On the top of the controller I added
use Cake\Cache\Cache;
use Cake\Cache\CacheEngine;
use Cake\Cache\CacheRegistry;
But when I used any function of memcache like Cache::write('variable','value'); etc
It is giving me error
" Error: Cache engine Cake\Cache\Engine\MemcachedEngine is not properly configured. "
The memcached is installed on server.. here is the output
/etc/php.d/memcache.ini,
memcache
memcache support => enabled
memcache.allow_failover => 1 => 1
memcache.chunk_size => 32768 => 32768
memcache.compress_threshold => 20000 => 20000
memcache.default_port => 11211 => 11211
memcache.hash_function => crc32 => crc32
memcache.hash_strategy => consistent => consistent
memcache.lock_timeout => 15 => 15
memcache.max_failover_attempts => 20 => 20
memcache.protocol => ascii => ascii
memcache.redundancy => 1 => 1
memcache.session_redundancy => 2 => 2
Please let me know what is the remaining in configuration. Thanks in advance.
Wrong extension
There are two php extensions related to memcache:
Memcache (older)
Memcached (newer, more features)
The CakePHP Memcached driver relies on memcacheD:
if (!extension_loaded('memcached')) {
return false;
}
Which will result in that error message if it's missing:
if (!$instance->init($config)) {
throw new RuntimeException(
sprintf('Cache engine %s is not properly configured.', get_class($instance))
);
}
Related
I have been trying and trying a lot but i just don't understand how to achieve this .. Here is what i want to achieve by the way..
I am using cakephp 3.0 and i am trying to append $this->request->data by trying below code in my controller...
$this->data['Leaveregisters'] = array(
'name'=>'another',
'surname'=>'whatever'
);
echo '<pre>';
print_r($this->request->data);
exit;
but i am keep getting below error
Notice (8): Indirect modification of overloaded property App\Controller\LeaveregistersController::$data has no effect
However, i am able to append data using below code
$this->request->data['name'] = 'another';
$this->request->data['surname'] = 'whatever';
But i am just wondering why i am not able to achieve the same thing using array() or what should i do to achieve this ?
Array should be appened in the same array as well if i do $this->request->data currently if i use below code
$this->request->data('Leaveregisters', [
'name' => 'another',
'surname' => 'whatever asdfa'
]);
echo '<pre>';
print_r($this->request->data);
exit;
Its getting appeneded but with new key like below which i do not want
Array
(
[leaveregister_user_id] => 2
[leaveregister_num_leaves] => 15
[leaveregister_sdate] => 2016-09-01
[leaveregister_edate] => 2016-09-22
[leaveregister_leave_type] => 1
[leaveregister_status] => 1
[leaveregister_created_date] => 2016-09-20 14:54:29
[leaveregister_modified_date] => 2016-09-20 14:54:29
[Leaveregisters] => Array
(
[name] => another
[surname] => whatever asdfa
)
)
I want something like this instead...
Array
(
[leaveregister_user_id] => 2
[leaveregister_num_leaves] => 15
[leaveregister_sdate] => 2016-09-01
[leaveregister_edate] => 2016-09-22
[leaveregister_leave_type] => 1
[leaveregister_status] => 1
[leaveregister_created_date] => 2016-09-20 14:54:29
[leaveregister_modified_date] => 2016-09-20 14:54:29
[name] => another
[surname] => whatever asdfa
)
So that i can just save my data using $this->request->data
Can someone guide me for the same ?
Thanks
There is no Controller::$data property anymore in CakePHP 3.
Cookbook > Appendices > 3.X Migration Guide > 3.0 Migration Guide > Controller
The only correct way is to access the request object, and of course you can set nested data if you want to the same way, ie:
$this->request->data['Leaveregisters'] = [
'name' => 'another',
'surname' => 'whatever'
];
Alternatively use the data() method:
$this->request->data('Leaveregisters', [
'name' => 'another',
'surname' => 'whatever'
]);
See also
Cookbook > Request & Response Objects > Request Body Data
API > \Cake\Network\Request::data()
I am trying to use redis as my caching engine and I have so far was able to add the add redis in cakephp and it writes my sessions to my redis just fine. This is what I did in the core
$engine = 'Redis';
$prefix = 'myapp_';
Cache::config('session', array(
'engine' => $engine,
'prefix' => $prefix . 'cake_session_',
'duration' => $duration
));
Configure::write('Session', array(
'defaults' => 'cache',
'timeout' => 100,
'start' => true,
'checkAgent' => false,
'handler' => array(
'config' => 'session'
)
));
Now I want to be able to write to redis from my controller some cached queries but I am not able to save that in redis. Instead it saves it to some default (I have no idea where) cache.
This is what I did in my controller
public function index()
{
Cache::write("TestKey","myquery","default");
var_dump(Cache::read("TestKey")); die;
}
The above gives me the value myquery on the browser but when I go to my redis-cli and look for keys * I do not see that key there. So obviously its saving somewhere else. I am not sure how to make it to write to redis. I have tried this Cache::write("TestKey","myquery","Redis"); but that also did not work.
Thanks for help in advance
**EDIT 1 **
I just did some debugging and it looks like its trying to add to file
object(FileEngine)[3]
protected '_File' => null
public 'settings' =>
array (size=10)
'engine' => string 'File' (length=4)
'path' => string '/Library/WebServer/Documents/phoneapp_backend/app/tmp/cache/' (length=60)
'prefix' => string 'cake_' (length=5)
'lock' => boolean true
'serialize' => boolean true
'isWindows' => boolean false
'mask' => int 436
'duration' => int 3600
'probability' => int 100
'groups' =>
array (size=0)
empty
protected '_init' => boolean true
protected '_groupPrefix' => null
How do I change it to write to redis?
Thanks
You need to configure the default cache configuration to use Redis insteado of File. In app/Config/bootstrap.php Change
Cache::config('default', array('engine' => 'File'));
So it reads:
Cache::config('default', array('engine' => 'Redis'));
You may need to add more options to the configuration for specifying the server and port where Redis is running.
When you retrieve records using $this->paginate('Modelname') with some page limit, how do you get the total number of records retrieved?
I'd like to display this total count on the view, but count($recordsRetrieved) returns the number displayed only on the current page. So, if total number of records retrieved is 99 and limit is set to 10, it returns 10, not the 99.
You can debug($this->Paginator->params());
This will give you
/*
Array
(
[page] => 2
[current] => 2
[count] => 43
[prevPage] => 1
[nextPage] => 3
[pageCount] => 3
[order] =>
[limit] => 20
[options] => Array
(
[page] => 2
[conditions] => Array
(
)
)
[paramType] => named
)
*/
The final code for PHP >=5.4:
$this->Paginator->params()['count'];
For PHP versions less than 5.4:
$paginatorInformation = $this->Paginator->params();
$totalPageCount = $paginatorInformation['count'];
To get your answer go to he following link
http://book.cakephp.org/2.0/en/controllers/request-response.html
use pr($this->request->params) you will find all the stuff of pagination
For cake 3.x
you can use method getPagingParams
example:
debug($this->Paginator->getPagingParams());
output:
[
'users' => [
'finder' => 'all',
'page' => (int) 1,
'current' => (int) 5,
'count' => (int) 5,
'perPage' => (int) 1,
'prevPage' => false,
'nextPage' => true,
'pageCount' => (int) 5,
'sort' => null,
'direction' => false,
'limit' => null,
'sortDefault' => false,
'directionDefault' => false,
'scope' => null
]
]
Here's how I got the count from my controller*
This is for CakePHP 2.3. It uses a view helper, which is typically a no-no in the controller as it violates MVC, but in this case I think makes sense to keep the code DRY.
// Top of file
App::uses('PaginatorHelper', 'View/Helper');
// Later in controller method
$paginatorHelper = new PaginatorHelper(new View(null));
$records = $this->paginate();
$count = $paginatorHelper->params()['count'];
* I know the OP asked about from the view, but I figure if Arzon Barua's answer is helping people (though I think it only tells you the requested count, not the actual count as the OP wants), then this might help too.
This way is getting a pagination information over controller.
class YourController extends Controller{
$helpers = array('Paginator');
public fn(){
$view = new View(null);
var_dump($view->paginator->params());
}
}
In my controller, i fetched the values from db using doctrine in array format. The array look like this
Array(
[0] => Admin_Model_prod Object
(
[_id:protected] => 1
[_pname:protected] => mobile
[_categoryname:protected] => device
[_price:protected] => 10000
[_status:protected] => sold
)
[1] => Admin_Model_prod Object
(
[_id:protected] => 2
[_pname:protected] => tv
[_categoryname:protected] => device
[_price:protected] => 50000
[_status:protected] => sold
)
)
now i want to display the values with id=1..
any suggestion pls
$userId = Doctrine_Query::create()
->select('o.authorId')
->from("Offer o")
->where("o.id =$offerId")
->fetchArray();
Use your Query like this. you will get an array instead of object
So here is my latest issue:
I run this query in my Cakephp controller:
$acctRenewLast2Entries = $this->AccountRenew->find
(
'all',
array
(
'conditions' => array('Acc_Id' => $plan["Account"]["Acc_Id"]),
'order' => array('AccR_Id' => 'DESC')
)
);
I am expecting 4 records for this SQL statement. Instead, on running Debug in my controller, this is what I get for each row for above (see first record):
app/controllers/admins_controller.php (line 2584)
1
app/controllers/admins_controller.php (line 2584)
Array
(
[AccountRenew] => Array
(
[AccR_Id] => 470
[AccR_Date] => 2012-06-23 01:21:11
[AccR_Hstart_Date] => 2012-06-23 01:21:11
[AccR_Hend_Date] => 2012-08-23 01:21:11
[AccR_End_Date] => 2013-08-23 01:21:11
[AccR_Status] => PAID
[AccR_Reason] => RENEWAL
[Inv_Id] => 467
[Inac_Id] =>
[Acc_Id] => 196
[AccT_Id] => 44
[Amount] => 16
[AccP_Id] => 0
)
)
app/controllers/admins_controller.php (line 2584)
Array
(
[AccountRenew] => Array
(
[AccR_Id] => 465
[AccR_Date] => 2012-06-23 01:17:35
[AccR_Hstart_Date] => 2012-06-23 01:17:35
[AccR_Hend_Date] => 2012-07-23 01:17:35
[AccR_End_Date] => 2012-07-23 01:17:35
[AccR_Status] => PAID
[AccR_Reason] => RENEWAL
[Inv_Id] => 462
[Inac_Id] =>
[Acc_Id] => 196
[AccT_Id] => 41
[Amount] => 16
[AccP_Id] => 0
)
)
app/controllers/admins_controller.php (line 2584)
Array
(
[AccountRenew] => Array
(
[AccR_Id] => 269
[AccR_Date] => 2012-06-06 10:15:56
[AccR_Hstart_Date] => 2012-06-06 17:15:56
[AccR_Hend_Date] => 2012-06-20 17:15:56
[AccR_End_Date] => 2012-06-20 10:15:56
[AccR_Status] => TRIAL
[AccR_Reason] =>
[Inv_Id] => 0
[Inac_Id] =>
[Acc_Id] => 196
[AccT_Id] => 0
[Amount] => 0
[AccP_Id] => 0
)
)
Now, when I run sql_dump, I get the following query that was run :
SELECT `AccountRenew`.`AccR_Id`, `AccountRenew`.`AccR_Date`, `AccountRenew`.`AccR_Hstart_Date`, `AccountRenew`.`AccR_Hend_Date`, `AccountRenew`.`AccR_End_Date`, `AccountRenew`.`AccR_Status`, `AccountRenew`.`AccR_Reason`, `AccountRenew`.`Inv_Id`, `AccountRenew`.`Inac_Id`, `AccountRenew`.`Acc_Id`, `AccountRenew`.`AccT_Id`, `AccountRenew`.`Amount`, `AccountRenew`.`AccP_Id` FROM `account_renews` AS `AccountRenew` WHERE `Acc_Id` = 196 ORDER BY `AccR_Id` DESC 4 4
And when I run the above query in MySQL, I do get all my 4 records, including the first one which in the array appears as 1 (way up top of my write-up).
I sincerely hope someone out there can help, because I spent the last 1.5 days without any luck as to why MySQL pulls up the complete set, but Cake only seems to retrieve the last 3, and replace the first record with an array of "1".
Thanks in advance!
#user996302 this issue seems remotely connected to one of my Cake Lighthouse tickets you pointed out.
#GMOAdmin I suspect that there could be a problem with the name of your model as the word "renew" is a verb and since it has no plural form this could somehow be obstructing the CakePHP conventions as the Inflector class may not be able to translate this.
The correct noun is renewal and it's plural form: renewals. You can try renaming (according to the convetions) the DB table, Model - name and classname, Controller - name and classname and see if that works.
You can test if Inflector is handling this correctly via the following Inflector methods:
Inflector::pluralize($singular), Inflector::classify($tableName), Inflector::tableize($camelCase)
A quick fix would be to issue the working query with $this->ModelName->query($queryToRun); this way you can go around this since, as you say, the query runs correctly when ran over the DB. Overall this is truly and interesting issue and I suggest you have the CakeCore team look at it - if it is reproducable then this is a BUG and it need fixin.
Very strange problem.
I don't know what's happening, but I read recently a bug report which sounds a lot like your problem: ticket.
You may try as suggested by the author: put the param $showHTML (cake docs) as true:
debug($var, true, true);
Hope that helps.
Okay, I think I have found the answer. At least, this was causing the problems for me.
The CakePHP counterCache
What I had:
public $hasMany = array(
'Flight' => array(
'className' => 'Flight',
'foreignKey' => 'user_plane_id',
'counterCache' => true
)
);
Very stupid, but it did cause the problems. So perhaps you are having a counterCache as well? Or maybe you have set another key in the relationships wrong?
If you don't see the problem at my code snippet. I should have added the counterCache to the $belongsTo instead of the $hasMany. So it would be something like this:
public $belongsTo = array(
'UserPlane' => array(
'className' => 'UserPlane',
'foreignKey' => 'user_plane_id',
'counterCache' => true
)
);
note: I am running CakePHP 2.x and not 1.3!