In CDetailView, how do I link a give value to another page? Say I have a book that is member of a book category, e.g. Science Fiction. The book details page displays the following data:
Book title: Dune
Author: Frank Herbert
Category: Science Fiction --> link
Science Fiction should be a link to /category/view, like in the following code, which doesn't work for me:
array(
'label' => 'Category',
'value' => $model->category->name,
'urlExpression'=>'Yii::app()->createUrl("category/view",
array("cid"=>$data->category_id))',
'class'=>'CLinkColumn',
),
I'm getting the impression that urlExpression does not work with CDetailView. It is a property of class CLinkColumn (» CGridColumn » CComponent), so I'm on the wrong track here. What is the proper way to handle this?
$this->widget('CDetailView', array(
'data'=>$model,
'attributes'=>array(
'title',
'author',
array(
'name'=>'Category',
'type'=>'raw',
'value'=>CHtml::link('Link Title', 'url'),
),
...
You are confusing CDetailView and CGridView.
urlExpression is a CLinkColumn attribute, and CLinkColumns are used in CGridViews's columns attribute.
CDetailView only has an attributes attribute.
I guess you could generate your url in the value attribute and use type = raw to display it
Related
I am using ORM in FuelPHP to fetch some data on a table view I created in my data base.
I followed the instruction that is given here but I'm getting an error as seen on the title above. Here is a screenshot of the error for reference (http://prntscr.com/72ssqc).
Here is the code:
http://pastebin.com/ips5VCzV
Here is a screenshot of the view Table:
http://prntscr.com/72st1e
Your view definition is wrong, it misses the 'columns' array, as the message explains.
protected static $_views = array(
'hugot_summary' => array(
'columns' => array(
'id',
'user_id',
'photo_id',
'hugot',
'url',
'comment_count',
'upvotes',
'created_at',
'updated_at'
),
),
);
I'm using Yii and I'm having a little problem with some dropdowns.
Basically I'm using CForm to display some dropdown menus of courses. A student can select up to two courses and for each course choice the student can select a 1st choice and second. It is a requirement that each course choice is inserted separately into the database. For example, it a student wants to study 2 courses and wants to have a 1st and 2nd priority course, they would choose like this:
Course one - 1st Priority
Course one - 2nd Priority
Course two - 1st Priority
Course two - 2nd Priority
This would put 4 new rows into the database. The administrators of the courses want this displayed as 4 dropdown menus containing the courses.
At the moment, I'm testing with just the 1st and 2nd priorities of course one, but the problem is that course one - priority one is always empty unless a value is selected for priority two. Then priority one gets the same value as priority two, even though two different courses are selected. I've been following this tutorial Form Builder as I am using the Wizard Behavior which uses CForm to build the forms.
Here is my code so far, again only dealing with "course one":
This is a snippet of the relevant code from the controller:
// inside controller
$model = new CourseChoice();
$form = new CForm('application.views.wizard.ccForm', $model);
$form['courseOneP1']->model = new CourseChoice();
$form['courseOneP2']->model = new CourseChoice();
$c1p1 = $form['courseOneP1']->model;
$c1p2 = $form['courseOneP2']->model;
// Here I am just reading the attributes and exiting for testing
if ($form->submitted()&& $form->validate()) {
echo '<pre>';
print_r($c1p1->attributes);
print_r($c1p2->attributes);
echo '</pre>';
exit;
..........
And here is code in the form in ccForm
return array(
'showErrorSummary' => true,
'title' => 'Course Choice 1',
'elements' => array(
// Course 1 - 1st Priority
'courseOneP1' => array(
'type' => 'form',
'elements' => array(
'course' => array(
'label' => '1st Priority',
'type' => 'dropdownlist',
'id' => 'c1p1',
'prompt' => 'Select 1st Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
),
),
// Course 1 - 2nd Priority
'courseOneP2' => array(
'type' => 'form',
'elements' => array(
'course' => array(
'label' => '2nd Priority',
'type' => 'dropdownlist',
'id' => 'c1p2',
'prompt' => 'Select 2nd Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
),
),
),
'buttons' => array(
'previous' => array(
'type' => 'submit',
'label' => 'Previous'
),
'submit' => array(
'type' => 'submit',
'label' => 'Next'
)
)
);
So lets say I choose 2 courses, one with an id of 15 and the other with an id of 86, I get the following when I print_r() both dropdowns:
Array // Dropdown 1
(
[course] => 86
.... // other irrelevant attributes
)
Array // Dropdown 2
(
[course] => 86
.... // other irrelevant attributes
)
Update
I've been looking further into this and when I look at firebug, I see that both dropdowns have the same name:
<div class="row field_course">
<label for="c1p1">1st Priority</label>
<select id="c1p1" name="CourseChoice[course]">
</div>
<div class="row field_course">
<label for="c1p2">2nd Priority</label>
<select id="c1p2" name="CourseChoice[course]">
</div>
So the 2nd menu is overwriting the first. But how can I change this? If I change 'course'=>array(.... in the CForm for either subform, the applicable dropdown does not render. I have already tried adding 'name'=>'course1' in the form but it makes no difference.
Couldn't you just set the name of the 2nd priority input element?
'course' => array(
'label' => '2nd Priority',
'name' => 'course2',
'type' => 'dropdownlist',
'id' => 'c1p2',
'prompt' => 'Select 2nd Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
Just to answer my own question and close it as it is pretty old now, CForm does not support tabular input and would need to extended to achieve this. Probably not a big job but in the end I convinced management that the four dropdowns design was horrible. :-) I went with a more flexible design showing a gridview of courses in a pop-up to choose courses instead which works well and is less confusing for the user.
Anyone interested in this issue can see the open issue here. There is a link in there to view a possible implementation of extending CForm, though this was posted at the end of 2009.
I need to restrict a category to a set of countries in Prestashop 1.5.
This restriction would prevent the shipping of a product belonging to such a category; as such, the users would still be able to see the products but they would not be able to buy them.
Ideally, I wanted to develop a module that would insert a list of countries (checkbox style, as in the Modules -> Payment page (AdminPayment)) inside a category's edit page, but I haven't been able to do so.
Why can't i simply paste the following code inside the renderForm() function?
Only the description is visible if i do so...
array(
'items' =>Country::getCountries(Context::getContext()->language->id),
'title' => $this->l('Country restrictions'),
'desc' => $this->l('Please mark the checkbox(es) for the country or countries for which you want to block the shipping.'),
'name_id' => 'country',
'identifier' => 'id_country',
'icon' => 'world',
),
EDIT:
I managed to get the list of countries working:
array(
'type' => 'checkbox',
'label' => $this->l('Restricted Countries').':',
'class' => 'sel_country',
'name' => 'restricted_countries',
'values' => array(
'query' => Country::getCountries(Context::getContext()->language->id),
'id' => 'id_country',
'name' => 'name'
),
'desc' => $this->l('Mark all the countries you want to block the selling to. The restrictions will always be applied to every subcategory as well')
),
Now, I can save these values by checking if the value "submitAddcategory" is being submitted in the postProcess function and by running an insert query there. Similarly, I can also load the IDs of the blocked countries from the database, but how can I tick the respective select boxes in the list of countries?
My initial "quick and dirty" idea was to use jQuery selectors inside a document.ready(), but the code gets inserted before everything else and, as such, it won't work because jQuery isn't even loaded yet.
How can this be done?
Cheers
I solved it by using the following code right before the end of the renderForm() function.
The Pièce de résistance was $this->fields_value, as sadly I didn't known of its existence.
public function getRestrictedCountries($obj)
{
// Loading blacklisted countries
$country = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT DISTINCT id_country
FROM `'._DB_PREFIX_.'category_country_restriction`
WHERE id_category = ' . (int)Tools::getValue('id_category') . ';');
$blacklisted_countries = array();
if (is_array($country))
foreach ($country as $cnt)
$blacklisted_countries[] = $cnt['id_country'];
// Global country list
$c_todos = Country::getCountries(Context::getContext()->language->id);
// Crossmatching everything
foreach ($c_todos as $c)
$this->fields_value['restricted_countries_'.$c['id_country']] = Tools::getValue('restricted_countries_'.$c['id_country'], (in_array($c['id_country'], $blacklisted_countries)));
}
PS: The table I am reading from is basically an associative table between 'category' and 'country'
I am using zii.widgets.grid.CGridView.....i want to add new coulmn in this.which is not in my database.how to add this column so that it show in listing
Just add a new column and stablish the value by hand:
'columns'=>array(
'something',
array(
'header'=> 'Custom',
'type' => 'raw',
'value' => '"something"'
),
),
These two articles may help:
CGridView reference:
http://www.yiiframework.com/doc/api/1.1/CGridView
CGridView - render custom columns:
http://www.yiiframework.com/wiki/278/cgridview-render-customized-complex-datacolumns/
I have the following tables.
User:
---------
id<br>
firstName
Project:
---------
id<br>
Name
StaffingManager
---------------
id<br>
User_id(FK)<br>
Total_Staff<br>
StaffingProjectMonth
-----------------------
id<br>
Project_id(FK)<br>
StaffingManager_id(FK)<br>
I want to define the relations in StaffingProjectMonth model
This is the default relation defined by YII using gii
public function relations()
{
return array(
'project' => array(self::BELONGS_TO, 'Project', 'Project_id'),
'staffingManager' => array(self::BELONGS_TO, 'StaffingManager', 'StaffingManager_id'),
);
}
I was able to get the ProjectName and search by ProjectName.
I want to get the UserfirstName and search by that.
I defined the relation this way.
return array(
'project' => array(self::BELONGS_TO, 'Project', 'Project_id'),
'staffingmanager' => array(self::BELONGS_TO, 'StaffingManager', 'StaffingManager_id'),
'user'=> array(self::HAS_MANY,'User',array('User_id'=>'id'),'through'=>'staffingmanager' ),
);
and in search method I did this:
$criteria->with = array('project','user');
//$criteria->compare('id',$this->id);
$criteria->compare('Name',$this->Project_id,true);
$criteria->compare('firstName',$this->StaffingManager_id,true);
and in the view:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'staffing-project-month-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
// 'id',
array('name'=>'Project_id','header'=>'Project','value'=>'$data->project->Name',),
array('name'=>'StaffingManager_id','header'=>'User','value'=>'$data->staffingmanager->user->firstName',),
..............
only the search by project name works. Able to sess the UserfirstName but unable to search by the firstName. Some wrong in defining relations.
Any help is appreciated. Thanks.
I recommend the following excellent wiki article by redguy, that describes how to search by related model attributes:
Following the approach of this article:
You should declare two new variables in your model:
public $project_name;
public $staffingmanager_firstname;
You should declare these new variables as safe for search in the rules() method:
array( 'project_name,staffingmanager_firstname,...', 'safe', 'on'=>'search' ),
Your search() method criteria should look like this:
$criteria->compare('project.Name',$this->project_name,true);
$criteria->compare('user.firstName',$this->staffingmanager_firstname,true);
In your view file, the 'columns' should be:
array('name'=>'project_name','header'=>'Project','value'=>'$data->project->Name',),
array('name'=>'staffingmanager_firstname','header'=>'User','value'=>'$data->user->firstName',),
Hope this helps.
Best regards...
I had troubles with $criteria->with and ”through“ relation.
Thing was in relation kind. BELONGS_TO doesn't work correct. I switched it to HAS_ONE and inverted id direction — array('parent_id'=>'id'). Now it's working!