I'm having some troubles setting up the routes for my restful api in zend framework2. I want to create route in this format: api-rest/wall/user_id/post[/:id].
Now this is my config file:
'router' => array(
'routes' => array(
'api-rest' => array(
'type' => 'segment',
'options' => array(
'route' => '/api-rest/wall[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'ApiRest\Controller\Wall',
),
),
),
),
),
You can try something like this:
'router' => array(
'routes' => array(
'api-rest' => array(
'type' => 'segment',
'options' => array(
'route' => '/api-rest/wall/:userid/posts[/:id]',
'constraints' => array(
'id' => '[0-9]+',
'userid' => '[0-9]+',
),
'defaults' => array(
'controller' => 'ApiRest\Controller\Wall',
),
),
),
),
),
In this configuration userid is mandatory, not optional.
/api-rest/wall/3/posts/2 - Post with id 2 and user with id 3
/api-rest/wall/3/posts - All posts by user with id 3
/api-rest/wall/3 - Won't match
You may also want to take a look child route usage in routing documentation.
Related
I'm trying to join 3 tables using cakephp but I don't understand why when I execute my query there is the result of the main tables without the attributes from the other 2 tables . this is my php code :
$flightSchedule = $this->FlightSchedules->find('all', array(
'join' => array(
array(
'table' => 'WeeklySchedules',
'alias' => 'ws',
'type' => 'INNER',
'conditions' => array(
'FlightSchedules.code = ws.flight_plan_code'
),
array(
'table' => 'Structures',
'alias' => 's',
'type' => 'LEFT',
'conditions' => array(
'FlightSchedules.code = s.flight_plan_code',
)),
)),
'conditions' => array(
'FlightSchedules.plane_code' => 'xxx'
),
));
the result of this is always the FlightSchedules record without the join of the other tables . Any ideas how to fix this ?
use "fields" for display the data of an associated table .
$flightSchedule = $this->FlightSchedules->find('all', array(
'join' => array(
array(
'table' => 'WeeklySchedules',
'alias' => 'ws',
'type' => 'INNER',
'conditions' => array(
'FlightSchedules.code = ws.flight_plan_code'
),
array(
'table' => 'Structures',
'alias' => 's',
'type' => 'LEFT',
'conditions' => array(
'FlightSchedules.code = s.flight_plan_code',
)),
)),
'conditions' => array(
'FlightSchedules.plane_code' => 'xxx'
),
'fields'=>'FlightSchedules.*,ws.*,s.*'
));
Add this in after condition array
'fields' => 'FlightSchedules.*, WeeklySchedules.*, Structures.*'
So I have created records and each has their own particular Service field. They are all filled up (not null).
In the MySQL database, there are also records of that field Service
But in my CGridView, my Service field is all blank. All the Service fields are blank for all the records. When I click on the button View - The one with the magnifying glass, it says Service - Not Set
Can I know what may cause the problem? Please help me Thanks!
UPDATED WITH CGRIDVIEW CODE
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'booking-grid',
'htmlOptions'=>array('class'=>'grid-view grid-size'),
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name'=>'date',
'htmlOptions'=>array('style'=>'text-align:center', 'height'=>'50px', 'width'=>'80px'),
'headerHtmlOptions'=>array('height'=>'30px'),
),
array(
'name'=>'timeStart',
'header'=>"Time",
'htmlOptions'=>array('style'=>'text-align:center', 'width'=>'80px'),
),
array(
'name'=>'client_id',
'value'=>'GxHtml::valueEx($data->client)',
'filter'=>GxHtml::listDataEx(Client::model()->findAllAttributes(null, true)),
'htmlOptions'=>array('width'=>'200px'),
),
array(
'name'=>'service_id',
'value'=>'GxHtml::valueEx($data->service)',
'filter'=>GxHtml::listDataEx(Service::model()->findAllAttributes(null, true)),
),
array(
'name' => 'complete',
'header'=>'Status',
'value' => '($data->complete == 0) ? Yii::t(\'app\', \'Open\') : Yii::t(\'app\', \'Close\')',
'filter' => array('0' => Yii::t('app', 'Open'), '1' => Yii::t('app', 'Close')),
'htmlOptions'=>array('width'=>'60px', 'style'=>'text-align:center'),
),
'remarks',
array(
'class' => 'CButtonColumn',
'visible'=>true,
'htmlOptions'=>array('width'=>'70px'),
'template' => '{view}{update}{delete}{changeComplete}',
'buttons'=>array(
'view' => array(
'visible'=>"true",
),
'update' => array(
'visible'=>"true",
),
'delete' => array(
'visible'=>"UserIdentity::isRoleMember('Admin')",
),
'changeComplete' => array(
'visible' => '$data->complete=="0"',
'imageUrl' => 'images/complete.png',
'options' => array(
'title' => 'Complete',
),
'url' => 'Yii::app()->createUrl("booking/changeComplete", array("id"=>$data->id))',
'click' => "function(){
$.fn.yiiGridView.update(\"booking-grid\", {
type:'POST',
url:$(this).attr('href'),
success:function(data) {
$.fn.yiiGridView.update(\"booking-grid\");
}
})
return false;
}",
),
),
),
))); ?>
I have tried looking at other CGridViews with the same field, they look the same and they're working perfectly fine.
I think the problem is $data->service. Probably your data has a relation with the Service entity. So for accessing attributes of Service you should use $data->service->attributeName.
Note- First, Read the question carefully.
I am new in ZF2. Right now i am working on zend framework 2.3. I have three module in this project and i am working on Wamp.
Application ----------- // Default ZF2 Page.
Album ------------// ZF2 Tutorial Example
Html ------------// I have Created
I have created a custom layout for Html module (last one). But there is a issue of basePath. I am not getting the right page path in this module. so i change in Html/config/module.config.php and add following script in module.config.php
'view_manager' => array(
'base_path' => 'zend/public/htmlmodule/',
'template_path_stack' => array(
'html' => __DIR__ . '/../view',
),
),
Now, i am getting the right basepath for this module. But now basepath automatically change for two other module such as album and application. Now i deleted the above code and add this code in zend\config\autoload\local.php
view_manager' => array(
'base_path' => 'zend/public/htmlmodule/',
'template_path_stack' => array(
'html' => __DIR__ . '/../view',
),
),
Now this script had change path for all module.
Now, My question is, i want change a basepath for a particular module and layout(optional), not for all module and want different basepath for differ module. Actually without right basepath, you will not get the right image/css and js file.
Here I am sharing my module.config.php file for all 2 module.
<?php
//Album Module > module.config.php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'base_path' => 'zend/public/album',
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
and HTML Module's module.config.php is:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Html\Controller\Html' => 'Html\Controller\HtmlController',
),
),
'module_layouts' => array(
'Application' => 'layout/layout.phtml',
'Html' => 'layout/custom.phtml',
),
'router' => array(
'routes' => array(
'html' => array(
'type' => 'segment',
'options' => array(
'route' => '/html[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Html\Controller\Html',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'base_path' => 'zend/public/htmlmodule/',
'template_path_stack' => array(
'html' => __DIR__ . '/../view',
),
),
);
I want know, Where am i doing wrong and how i will get the right base path according module.
Thanks to all in advance for contribution in this question.
Add specific base_path keys to each view manager as you have done so then try using $this->getHelper('basePath')->setBasePath() for the viewModels you generate just to make sure.
As a caveat, I avoided this problem entirely by making the base path the "lowest point" in the tree, in your case:
'view_manager' => array(
'base_path' => 'zend/public/',
'template_path_stack' => array(
'html' => __DIR__ . '/../view',
),
),
and then adding module specific path sections in module specific views. So if I wanted to access the album specific css or js files in the album folder under public then in my view I'd reference the base path and simply add "/album" before the rest of the path. Less headache for you I believe if you do it this way.
This code:
$conditions = array(
'fields' => array(
'User.id'
),
'conditions' => array(
'AND' => array(
'UsersProblem.problem_id' => 38,
'UsersProblem.problem_id' => 34,
),
),
'recursive' => -1
);
$conditions['joins'][] = array(
'table' => 'users_problems',
'alias' => 'UsersProblem',
'type' => 'INNER',
'conditions' => array(
'User.id = UsersProblem.user_id',
));
Is transforming to this SQL query:
SELECT `User`.`id`
FROM `lawyers`.`users`
AS `User`
INNER JOIN `lawyers`.`users_problems`
AS `UsersProblem`
ON (`User`.`id` = `UsersProblem`.`user_id`)
WHERE `UsersProblem`.`problem_id` = 34
Where is AND UsersProblem.problem_id = 38"?
How to create correct find condition to get users with some list of problems? Which are linked as many to many relationship.
Using $this->Problem->find is not possible, beause I need to use 2 joins: users_problems and users_practices. And use for them AND condition, like this:
'AND' => array(
'UsersProblem.problem_id' => 38,
'UsersProblem.problem_id' => 34,
'UsersPractices.practice_id' => 1,
'UsersPractices.practice_id' => 2,
),
You're overwriting 'UsersProblem.problem_id'. Just think about it like the array it is. CakePHP is just using the PHP array structure to do these finds, and that means it follows array rules. But to get around that in cakephp, you're supposed to use a two dimensional array
Like this:
'AND' => array(
array('UsersProblem.problem_id' => 38),
array('UsersProblem.problem_id' => 34)
),
Instead of this:
'AND' => array(
'UsersProblem.problem_id' => 38,
'UsersProblem.problem_id' => 34
),
That way, instead of trying to set the key 'UsersProblem.problem_id' twice, you're making an array that looks like
0 -> array('UsersProblem.problem_id' => 38)
1 -> array('UsersProblem.problem_id' => 34)
And that will work, it's a workaround the CakePHP guys built into the system for this kind of situation.
I think you need to use OR instead of AND:
$conditions = array(
'fields' => array(
'User.id'
),
'conditions' => array(
'OR' => array(
array('UsersProblem.problem_id' => 38),
array('UsersProblem.problem_id' => 34),
),
),
'recursive' => -1
);
You can also roll this up as follows:
$conditions = array(
'fields' => array(
'User.id'
),
'conditions' => array(
'OR' => array(
'UsersProblem.problem_id' => array(34, 38)
),
'recursive' => -1
);
Change your conditions array as shown below:
$options = array(
'fields' => array('User.id'),
'conditions' => array('UsersProblem.problem_id' => array(38,34)),
'recursive' => -1
);
$options['joins'] = array(
array(
'table' => 'users_practices',
'alias' => 'UsersPractices',
'type' => 'INNER',
'conditions' => array(
/CONDITION OF JOIN/
),
array(
'table' => 'users_problems',
'alias' => 'UsersProblem',
'type' => 'INNER',
'conditions' => array(
'User.id = UsersProblem.user_id',
)
);
From User Model:
$this->find('list',$options);
Do not use AND/ OR because they are not optimized for above case.
For reference:
MYSQL OR vs IN performance
and
CakePHP right way to do this (get value from setting's table)
**Try the code for join tables in cakephp.......**
$options = array('joins' => array(
array(
'table' => 'modelname1',
'alias' => 'modelName1',
'type' => 'LEFT',
'foreignKey' => false,
'conditions' => array('modelname1.id = modelname2.mid')
)
),
'fields' => array('modelName1.field1', 'modelName1.field2', 'modelName2.field1', 'modelName2.field2'),
'conditions' => array('modelname.id' => 1, 'modelname2.field1' => ''),
'limit' => 10, 'page' => 1);
$this->modelName2->find($options);
In my project one of the model named types having multiple relation to other models the criteria with part is giving below its like
$criteria->with = array(
'category',
'subCategory',
'accountsSupplierPriceDetails' => array(
'with' => 'serviceLevel'
)
);
the relationship goes like
types - category - relation(1-1)
types - subcategory - relation(1-1)
types - accountsSupplierPriceDetails - relation(1-1)
accountsSupplierPriceDetails - serviceLevel - relation(1-n)
my problem is how to display the names from each table in a grid. the result of (1-n) should be displayed in a dropdownbox. when i try to access the data from column it shows the error Trying to get property of non-object ($data->accountsSupplierPriceDetails->serviceLevel ->name). can anyone please help me.
Thanks in advance.
in my view i create a cgridview with 5 rows in which one row data is populated using dropdownlist.
<div style="width:700px; height:auto;">
<?php
$widget = $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'response-grid',
'dataProvider' => $pickdataset->pickCategorylistingForMain(),
'cssFile' => Yii::app()->baseUrl . '/media/css/gridview.css',
'summaryText' => '',
'ajaxUpdate' => 'response-grid',
'enablePagination' => true,
'pager' => array(
'class' => 'LinkPager',
'cssFile' => false,
'header' => false,
'firstPageLabel' => 'First',
'prevPageLabel' => 'Previous',
'nextPageLabel' => 'Next',
'lastPageLabel' => 'Last',
),
'columns' => array(
array(
'name' => 'id',
'header' => '#',
'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
array(
'type' => 'raw',
'name' => 'categoryExt',
'header' => 'Category',
),
array(
'type' => 'raw',
'header' => 'Sub Category',
'value' => '$data->subCategory->name',
),
array(
'type' => 'raw',
'name' => 'locationExt',
'header' => 'Location',
),
array(
'type' => 'raw',
'header' => 'Name',
'value' => 'CHtml::dropDownList($data->corporate_id."-".$data->category_id."-".$data->sub_category_id."-".$data->location_id,"",popDropBox($data->masterCategoryServiceLevels), array("class" => "listbox"), array("empty" => "Select a Location"))',
),
array(
'type' => 'raw',
'header' => 'Pick',
'value' => 'CHtml::image(Yii::app()->baseUrl . "/media/images/edit_icon.png",$data->corporate_id."-".$data->category_id."-".$data->sub_category_id."-".$data->location_id,array("title" => "Select this Combination"))',
),
),));
function popDropBox($data)
{
$list = array();
foreach ($data as $row)
{
$list[$row->serviceLevel->id] = $row->serviceLevel->name;
}
return $list;
}
?>
</div>
the relation in model is:
'masterCategoryServiceLevels' => array(self::HAS_MANY, 'MasterCategoryServiceLevel', 'category_template_id'),
'corporate' => array(self::BELONGS_TO, 'AccountsCorporate', 'corporate_id'),
'category' => array(self::BELONGS_TO, 'MasterCategory', 'category_id'),
'subCategory' => array(self::BELONGS_TO, 'MasterCategory', 'sub_category_id'),
'location' => array(self::BELONGS_TO, 'MasterLocation', 'location_id'),
the row is
array(
'type' => 'raw',
'header' => 'Name',
'value' => 'CHtml::dropDownList($data->corporate_id."-".$data->category_id."-".$data->sub_category_id."-".$data->location_id,"",popDropBox($data->masterCategoryServiceLevels), array("class" => "listbox"), array("empty" => "Select a Location"))',
),
and i use a function to create dropdown data is
function popDropBox($data)
{
$list = array();
foreach ($data as $row)
{
$list[$row->serviceLevel->id] = $row->serviceLevel->name;
}
return $list;
}
please comment if you have any doubt. i am free to share... have a nice day SO friends... wishes and prayers.