I have used the nestedSortable jQuery plugin (https://github.com/ilikenwf/nestedSortable) to order my categories with drag and drop. Every time a category is been ordered an Ajax request is going to the controller with a post of an array with the complete data of the categories. Everything is working as expected so for;
Array received in controller by Ajax post:
Array
(
[0] => Array
(
[item_id] =>
[parent_id] =>
[depth] => 0
[left] => 1
[right] => 8
)
[1] => Array
(
[id] => 1
[parent_id] =>
[depth] => 0
[left] => 2
[right] => 3
)
[2] => Array
(
[id] => 4
[parent_id] =>
[depth] => 0
[left] => 4
[right] => 5
)
[3] => Array
(
[id] => 2
[parent_id] =>
[depth] => 0
[left] => 6
[right] => 7
)
)
In my controller I have the following function to process:
public function reorder() {
$list = isset($_POST['_list']) ? json_decode($_POST['_list'], true) : null;
foreach ($list as $item) {
if (!empty($item['id'])) {
$category = $this->Categories->get($item['id']);
if (!empty($category)) {
empty($item['parent_id']) ? $category->parent_id = null : $category->parent_id = $item['parent_id'] ;
$category->lft = $item['left'];
$category->rght = $item['right'];
$this->Categories->save($category);
}
}
}
}
When I debug the categories before saving the lft and rght are set correctly, however after saving the lft and rght contain completly different values.
I assume that since TreeBehavior is implemented the save function is doing some checks and resetting lft and rght to other values? Anyway to skip this?
Related
I am fairly new to cakePHP, i am trying to work with phtagr which uses cakePHP. I am trying to fix a pagination problem it has.
when i see the output of $this->request->params['search'] it has the pagecount value.
function beforeRender($viewFile) {
$this->search->iniitialize();
if(isset($this->request->params['search'])){
$this->data = am($this->defaults, $this->request->params['search']);
$this->data['page'] = 1;
}
} else {
$this->data = $this->defaults;
}
}
printing $this->request->params['search'] gives
Array ( [pageCount] => 27683 [current] => 0 [nextPage] => 1 [prevPage] => [baseUri] => /explorer/query [afterUri] => [defaults] => Array ( [page] => 1 [pos] => [show] => 24 [sort] => -date [view] => small ) [data] => Array ( ) [page] => )
How can i find out where the pageCount value is set?
Just search the source files for the string 'search'. For example using grep:
grep -r "'search'" /path/to/phTagr/
Results:
...
Controller/Component/SearchComponent.php: $this->controller->request->params['search'] = $params;
...
ie it's being set in the search component.
I use cakephp framework ,in that i post parent and child model data but i want to remove triming from that. My demo code is
Array
(
[Currency] => Array
(
[Code] => fgfg
[Name] => fg
[Description] =>
[NoOfDecimalPrecision] => 3
[CurrencyName] => bnjmhj
[CurrencySymbol] => hjh
[DecimalPrecisionName] => bjmh
[DecimalPrecisionSymbol] =>
[CurrencyScaleType_058] => 368
[IsPrefixSymbol] => 0
[IsInActive] => 0
[IsExchangeCurrency] => 0
[RevisionNumber] =>
)
[CurrencyDenomination] => Array
(
[0] => Array
(
[LineNo] => 1
[RowState] => 435
[DenominationName] => jhgjk
[DenominationDetails] => kgk
[DenominationValue] => 1
[RevisionNumber] => 1
[DenominationSequence] => 1
)
)
)
In my code currency is parent model and currencydenimination in child model so i want in both case.my data post dyanamic not static.so please suggest solution.
In my cakephp i need to retrieve datas from database.
how can i display the table result in neat format.
Controller:
function MarketingTaskmanagement(){
$data['list'] = $this->TravancoAdmin->get_task_all();
$this->set($data);
$this->render('Marketing/taskmanagement');
}
Model:
function get_task_all(){
$users = $this->query('select * from tbl_tasks');
if($users){
return $users;
}else{
return 1;
}
}
View:
But it displays values as so many arrays:
Array ( [0] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 1 [task_title_mm] => ghjg [task_description_mm] => gjg [task_from_mm] => 09/04/2012 [task_to_mm] => 09/27/2012 ) ) [1] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 2 [task_title_mm] => hf [task_description_mm] => hfgh [task_from_mm] => 09/03/2012 [task_to_mm] => 09/27/2012 ) ) [2] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 3 [task_title_mm] => hf [task_description_mm] => hfgh [task_from_mm] => 09/03/2012 [task_to_mm] => 09/27/2012 ) ) [3] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 4 [task_title_mm] => hfh [task_description_mm] => fgh [task_from_mm] => 09/03/2012 [task_to_mm] => 09/20/2012 ) ) [4] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 5 [task_title_mm] => h [task_description_mm] => h [task_from_mm] => 09/04/2012 [task_to_mm] => 09/28/2012 ) ) [5] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 6 [task_title_mm] => hjk [task_description_mm] => hk [task_from_mm] => 09/05/2012 [task_to_mm] => 09/22/2012 ) ) [6] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 7 [task_title_mm] => v [task_description_mm] => v [task_from_mm] => 09/03/2012 [task_to_mm] => 09/28/2012 ) ) [7] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 8 [task_title_mm] => d [task_description_mm] => d [task_from_mm] => 09/03/2012 [task_to_mm] => 09/28/2012 ) ) [8] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 9 [task_title_mm] => f [task_description_mm] => d [task_from_mm] => 09/04/2012 [task_to_mm] => 09/27/2012 ) ) [9] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 10 [task_title_mm] => b [task_description_mm] => b [task_from_mm] => 09/05/2012 [task_to_mm] => 09/27/2012 ) ) )
In codeigniter there have something like this:
$users = $this->query('select * from tbl_tasks')->result();
Then it displays the correct array without any complication .
So how can i do this in cakephp?
And i need to display the all task_title_mm values from the table result in view page.
How can i do this?
Use the "find" method.
$this->ModelName->find('all');
or
$this->ModelName->find('list');
If you need less information from query, put:
$this->ModelName->recursive = -1;
before find method.
Since you expanded your question I'll elaborate a bit on the anwser.
It is a good practice to not use $this->query() where a native cake search would work.
if you don't have a model file for your tbl_tasks create one:
<?php
class Task extends AppModel {
var $name = 'Task';
var $displayField = 'task_title_mm';
var $useTable = 'tbl_tasks';
}
Load it inside your get_task_all() function. You can load a model dinamically inside another.
function get_task_all(){
//load the Task model on the fly if it isnt associated with this one
//loadModel won't work because we're inside a model.
//App:import might work, dunno.
$this->Task = ClassRegistry::init('Task');
$users = $this->Task->find('list');
return $users ? $users : 1;
}
find('list') works if you declare 'task_title_mm' as displayField otherwise you can use $this->Task->find('all', array('fields' => array('task_title_mm')));, but the result will be prefixed with ['Task'];
function get_task_all(){
$users = $this->query('select * from tbl_tasks') ....
Hey hey hey! Stop right there! That just gave me a heart attack.
Zero. Read the cookbook! No seriously, Stop! before you go any further I would suggest going through the cookbook atleast once. Do things the right way from the beginning is the way to go.
Use proper methods that cake has provided you to get the job done. SQL queries can get the job done just fine, but trust me using cake's inbuilt methods will benefit you long term and make your life much easier.
As per your query on how to display the data in table format, use cake bake: http://book.cakephp.org/1.3/view/1522/Code-Generation-with-Bake . The cake bake utility will generate the basic scaffold for your application controllers, models, views included. The default view templates of cake are really useful and has almost all the functionality built in, pagination etc.
Want a paginated result? No problem use paginate!
function index() {
$this->ModelAlias->recursive = -1;
$this->set('list', $this->paginate());
}
And voila! you have your paginated data inside $list readily available in your view. And if you have your views baked everything is nicely displayed in a table as you needed.
Don't want to fetch everything? Insert conditions into paginate like this:
function index() {
$this->ModelAlias->recursive = -1;
$this->paginate = array(
'conditions' => array('ModelAlias.tasks' => 'incomplete')
);
$this->set('list', $this->paginate());
}
Not only this you can include virtually any otherkey like 'fields', 'order', 'limit' that cake supports.
Once you start using cake's functions things start to get really awesome. For fetching related data you can use cake 'containable' behavior. This is just one thing of hundreds that cake does really well. I can't outline everything here, I insist please have a go at the cookbook http://book.cakephp.org
Hey I have got data in my variable using foreach($report as &rep)
note it is getting things from 4 different table in database
Now when I print my $rep I get the following:
Array
(
[Report] => Array
(
[id] => 246
[emp] => werock
[name] => werock
[organization] => cakephp
[customer] => great
)
[file] => Array
(
[0] => Array
(
[id] => 211
[report_id] => 246
[file_name] =>
[file_type] =>
[file_size] => 0
[file_error] => 4
[file_tag] => 0
)
)
[Engineer] => Array
(
[0] => Array
(
[id] => 232
[report_id] => 246
)
)
[Issue] => Array
(
[0] => Array
(
[id] => 118
[report_id] => 246
[date_created] => 2012-02-10
[status] => wait
)
[1] => Array
(
[id] => 119
[report_id] => 246
[date_created] => 2012-02-10
[status] => debug
)
[2] => Array
(
[id] => 120
[report_id] => 246
[date_created] => 2012-02-10
[status] => Completed
)
)
)
Now what I want to do is access Issues array and check how many array are there in it. in this case 3(0,1,2). and print the value of status for the last index in this case(2).
But when I do $rep['Issue']['status'] I get Undefined index: status. Can you tell me where I might be going wrong.
What about this:
echo $rep['Issue'][count($rep['Issue'])-1]['status'];
Let me know if it works.
Try this
$rep['Issue'][$x]['status']
Where $x is variable you can use in a loop to get values.
You are missing the second index. It should be:
$rep['Issue'][count($rep['Issue'])-1]['status'].
You could also try using Set::extract instead. The Set class is very useful for working with arrays.
$status = Set::extract('/Issue/.[:last]/status', $rep);
if(count($status)){
// $status[0] == the value of status which is 'Complete' in your example
} else {
// no issues
}
I tried another method if just thought of sharing.
$endEl[$rep['Report']['id'] ] = end($rep['Issue'] );
let me know if it's a good way.. it works
Array
(
[0] => Array
(
[auth_id] => 1
[auth_section] => Client Data Base
[auth_parent_id] => 0
[auth_admin] => 1
[sub] => Array
(
[0] => Array
(
[auth_id] => 2
[auth_section] => Client Contact
[auth_parent_id] => 1
[auth_admin] => 1
)
)
)
[1] => Array
(
[auth_id] => 6
[auth_section] => All Back Grounds
[auth_parent_id] => 0
[auth_admin] => ,4
[sub] => Array
(
[0] => Array
(
[auth_id] => 7
[auth_section] => Edit Custom
[auth_parent_id] => 6
[auth_admin] => 1
)
)
)
[2] => Array
(
[auth_id] => 20
[auth_section] => Order Mail
[auth_parent_id] => 0
[auth_admin] => 1
[sub] =>
)
}
When I process the sub inner array
for($in=0 ; $in < count($auth); $in++){
$autsub = $auth[$in]["sub"];
for($g=0 ; $g<count($autsub); $g++){
echo $autsub[$g]["auth_id"];
}
}
it shows this error
Fatal error: Cannot use string offset as an array.........
how can I avoid that :(
The problem is that the last entry in the array (2) does not have a sub array, but you're trying to access it anyway. You'll need to check if the entry exists and if it's an array before looping over it. Here an example using foreach:
foreach ($array as $auth) {
if (!empty($auth['sub']) && is_array($auth['sub'])) {
foreach ($auth['sub'] as $sub) {
if (!empty($sub['auth_id'])) {
echo $sub['auth_id'];
}
}
}
}
You can test the offset type with the is_array() function. If you want a better answer, post the processing code.
Test if $auth[$in] and $autsub[$g] are arrays.