oflodor
(Rdemattei)
November 13, 2012, 11:31am
1
Hi all…
I did my widget…its working well…i mean…i get all category and sub category…
now the problem its that i get "Undefined index" for categoryIds variable…
how can i pass a variable from inside a widget to the actionCreat function…?
thank you so much…this its the code:
WIDGET CLASS:
class TutteCategorie extends CWidget
{
private $_parent;
public function init()
{
$this->_parent = Category::model()->findAll('parent_id = 0');
}
public function getTutteCategorie()
{
return $this->_parent;
}
public function run()
{
// this method is called by CController::endWidget()
$this->render('tutteCategorie');
}
}
WIDGET VIEW
foreach($this->getTutteCategorie() as $parent):
{
$child= $parent->getChilds();
//var_dump($child);
echo $parent->nome ; echo "<br />";
echo CHtml::activeCheckBoxList(
$parent,
'categoryIds',
$parents = Chtml::listData(Category::model()->findAll('parent_id = :id',array(':id'=> $parent->id)),'id','nome') );
}
endforeach;
PostController
if($model->save())
foreach ($_POST['Post']['categoryIds'] as $categoryId) {
$postCategory = new PostCategory;
$postCategory->post_id = $model->id;
$postCategory->category_id = $categoryId;
if (!$postCategory->save()) print_r($postCategory->errors);
}
$this->redirect(array('view','id'=>$model->id));
}
this is my form view HTML
<div class="row">
Category 1 <input type="hidden" name="Category[categoryIds]" value="" id="ytCategory_categoryIds">
<span id="Category_categoryIds">
<input type="checkbox" name="Category[categoryIds][]" value="1" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Cat1 - Sub-Category 1</label>
<input type="checkbox" name="Category[categoryIds][]" value="2" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Cat1 - Sub-Category 2</label>
<input type="checkbox" name="Category[categoryIds][]" value="3" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Cat1 - Sub-Category 3</label>
<input type="checkbox" name="Category[categoryIds][]" value="4" id="Category_categoryIds_3"> <label for="Category_categoryIds_3">Cat1 - Sub-Category 4</label></span>
Category 2 <input type="hidden" name="Category[categoryIds]" value="" id="ytCategory_categoryIds">
<span id="Category_categoryIds">
<input type="checkbox" name="Category[categoryIds][]" value="7" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Cat2 - Sub-Category 1</label>
<input type="checkbox" name="Category[categoryIds][]" value="8" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Cat2 - Sub-Category 2</label>
<input type="checkbox" name="Category[categoryIds][]" value="9" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Cat2 - Sub-Category 3</label>
</span>
</div>
i want to save in the post_categories junction table the values stored in the sub categories field…
thank you all!!
konapaz
(Konapaz)
November 13, 2012, 12:46pm
2
Hi oflodor
where the message with undefined categoryIds occurs? On submit or in the first time form shows?
check with var_dump if $parent->getChilds() if exist the field categoryIds
oflodor
(Rdemattei)
November 13, 2012, 1:33pm
3
hi konapaz!
i get that code when i sumbit the form creat post
i put var_dump and i get:
array(4) { [0]=> object(Category)#104 (13) { ["categoryIds"]=> NULL ["_md":"CActiveRecord":private]=> object(CActiveRecordMetaData)#96 (5) { ["tableSchema"]=> object(CMysqlTableSchema)#91 (9) { ["schemaName"]=> NULL ["name"]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "category" ["rawName"]=> string(10) "`category`" ["primaryKey"]=> string(2) "id" ["sequenceName"]=> string(0) "" ["foreignKeys"]=> array(0) { } ["columns"]=> array(3) { ["id"]=> object(CMysqlColumnSchema)#92 (14) { ["name"]=> string(2) "id" ["rawName"]=> string(4) "`id`" ["allowNull"]=> bool(false) ["dbType"]=> string(7) "int(11)" ["type"]=> string(7) "integer" ["defaultValue"]=> NULL ["size"]=> int(11) ["precision"]=> int(11) ["scale"]=> NULL ["isPrimaryKey"]=> bool(true) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(true) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["nome"]=> object(CMysqlColumnSchema)#93 (14) { ["name"]=> string(4) "nome" ["rawName"]=> string(6) "`nome`" ["allowNull"]=> bool(false) ["dbType"]=> string(12) "varchar(255)" ["type"]=> string(6) "string" ["defaultValue"]=> NULL ["size"]=> int(255) ["precision"]=> int(255) ["scale"]=> NULL ["isPrimaryKey"]=> bool(false) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(false) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["parent_id"]=> object(CMysqlColumnSchema)#97 (14) { ["name"]=> string(9) "parent_id" ["rawName"]=> string(11) "`parent_id`" ["allowNull"]=> bool(false) ["dbType"]=> string(7) "int(11)" ["type"]=> string(7) "integer" ["defaultValue"]=> int(0) ["size"]=> int(11) ["precision"]=> int(11) ["scale"]=> NULL ["isPrimaryKey"]=> bool(false) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(false) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } } ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["columns"]=> array(3) { ["id"]=> ......................
i dont paste all the code because its very long but it seems that the variables are ok…thank you!!
konapaz
(Konapaz)
November 13, 2012, 1:42pm
4
oflodor:
hi konapaz!
i get that code when i sumbit the form creat post
i put var_dump and i get:
array(4) { [0]=> object(Category)#104 (13) { ["categoryIds"]=> NULL ["_md":"CActiveRecord":private]=> object(CActiveRecordMetaData)#96 (5) { ["tableSchema"]=> object(CMysqlTableSchema)#91 (9) { ["schemaName"]=> NULL ["name"]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "category" ["rawName"]=> string(10) "`category`" ["primaryKey"]=> string(2) "id" ["sequenceName"]=> string(0) "" ["foreignKeys"]=> array(0) { } ["columns"]=> array(3) { ["id"]=> object(CMysqlColumnSchema)#92 (14) { ["name"]=> string(2) "id" ["rawName"]=> string(4) "`id`" ["allowNull"]=> bool(false) ["dbType"]=> string(7) "int(11)" ["type"]=> string(7) "integer" ["defaultValue"]=> NULL ["size"]=> int(11) ["precision"]=> int(11) ["scale"]=> NULL ["isPrimaryKey"]=> bool(true) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(true) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["nome"]=> object(CMysqlColumnSchema)#93 (14) { ["name"]=> string(4) "nome" ["rawName"]=> string(6) "`nome`" ["allowNull"]=> bool(false) ["dbType"]=> string(12) "varchar(255)" ["type"]=> string(6) "string" ["defaultValue"]=> NULL ["size"]=> int(255) ["precision"]=> int(255) ["scale"]=> NULL ["isPrimaryKey"]=> bool(false) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(false) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["parent_id"]=> object(CMysqlColumnSchema)#97 (14) { ["name"]=> string(9) "parent_id" ["rawName"]=> string(11) "`parent_id`" ["allowNull"]=> bool(false) ["dbType"]=> string(7) "int(11)" ["type"]=> string(7) "integer" ["defaultValue"]=> int(0) ["size"]=> int(11) ["precision"]=> int(11) ["scale"]=> NULL ["isPrimaryKey"]=> bool(false) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(false) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } } ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["columns"]=> array(3) { ["id"]=> ......................
i dont paste all the code because its very long but it seems that the variables are ok…thank you!!
Before of foreach var_dump the $_POST[‘Post’] to see what variables are passed
foreach ($_POST[‘Post’][‘categoryIds’] as $categoryId) {
.....
oflodor
(Rdemattei)
November 13, 2012, 2:02pm
5
you mean in the PostController ? i put this
if($model->save())
var_dump ( $_POST['Post']) ;
foreach ($_POST['Post']['categoryIds'] as $categoryId) {
$postCategory = new PostCategory;
$postCategory->post_id = $model->id;
$postCategory->category_id = $categoryId;
if (!$postCategory->save()) print_r($postCategory->errors);
}
$this->redirect(array('view','id'=>$model->id));
}
and after i submit the form nothing its printed…only
Undefined index: categoryIds
konapaz
(Konapaz)
November 13, 2012, 2:15pm
6
oflodor:
you mean in the PostController ? i put this
if($model->save())
var_dump ( $_POST['Post']) ;
foreach ($_POST['Post']['categoryIds'] as $categoryId) {
$postCategory = new PostCategory;
$postCategory->post_id = $model->id;
$postCategory->category_id = $categoryId;
if (!$postCategory->save()) print_r($postCategory->errors);
}
$this->redirect(array('view','id'=>$model->id));
}
and after i submit the form nothing its printed…only
Undefined index: categoryIds
put the var_dump ($_POST[‘Category’]) or var_dump ($_POST) better, before of save() call
oflodor
(Rdemattei)
November 13, 2012, 2:22pm
7
konapaz
(Konapaz)
November 13, 2012, 2:32pm
8
check with var_dump ($_POST) ;
make your form accordingly of
http://www.yiiframework.com/doc/guide/1.1/en/form.view
konapaz
(Konapaz)
November 13, 2012, 5:28pm
10
oflodor:
thank you konapaz…
Welcome
Don’t feel uncomfortable if I/we dont find the solution directly!
Try to solve the problem byself and if you don’t find the solution, we will try one of us of this forum to give the answer
oflodor
(Rdemattei)
November 14, 2012, 8:29pm
11
KonApaz:
Welcome
Don’t feel uncomfortable if I/we dont find the solution directly!
Try to solve the problem byself and if you don’t find the solution, we will try one of us of this forum to give the answer
Hi…so i got what its the problem in the form:
this its the code of the widget:
CLASS:
class TutteCategorie extends CWidget
{
private $_parent;
public $categoryIds;
public function init()
{
$this->_parent = Post::model()->stampaCategorie('parent_id = 0');
}
public function getTutteCategorie()
{
return $this->_parent;
}
public function run()
{
$this->render('tutteCategorie', array($this->categoryIds));
}
}
VIEW
<?php
foreach($this->getTutteCategorie() as $parent):
{
echo CHtml::activeCheckBoxList(
$parent,
'categoryIds',
Chtml::listData(Post::model()->getChilds($parent->id),'id','nome') );
}
endforeach; ?>
the problem its that the html output is this:
<span id="Category_categoryIds">
<input type="checkbox" name="Category[categoryIds][]" value="1" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Name 1</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="2" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Name 2</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="3" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Name 3</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="4" id="Category_categoryIds_3"> <label for="Category_categoryIds_3">Name 4</label></span>
<span id="Category_categoryIds">
<input type="checkbox" name="Category[categoryIds][]" value="7" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Name 5</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="8" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Name 6</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="9" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Name 7</label>
</span>
and as you can see the variables are duplicated (Category_categoryIds_0,Category_categoryIds_1 ecc…)
so if i select the last 4 variables it works well and create the post
otherwise if i check the frist variables( froma Name 1 to 4) it doesn work…
the code should be like this:
<span id="Category_categoryIds">
<input type="checkbox" name="Category[categoryIds][]" value="1" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Aborto</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="2" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Eutanasia</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="3" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Famiglia</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="4" id="Category_categoryIds_3"> <label for="Category_categoryIds_3">Pedofilia</label></span>
<input type="checkbox" name="Category[categoryIds][]" value="7" id="Category_categoryIds_5"> <label for="Category_categoryIds_5">Aborto</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="8" id="Category_categoryIds_6"> <label for="Category_categoryIds_6">Europa</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="9" id="Category_categoryIds_7"> <label for="Category_categoryIds_7">Famiglia</label>
</span>
how can i get that ?!?
thank very much for your help…
luc
(Luc Pfister)
November 14, 2012, 9:01pm
12
my 2cts: maybe you can pass an extra htmlOption in the activeCheckBoxList like :
$base_id='my_custom_';
echo CHtml::activeCheckBoxList(
$parent,
'categoryIds',
Chtml::listData(Post::model()->getChilds($parent->id),'id','nome'),
array('id'=>$base_id.$parent->id);
By reading checkBoxList source code, I’m not sure that it is possible …
oflodor
(Rdemattei)
November 14, 2012, 10:24pm
13
luc:
my 2cts: maybe you can pass an extra htmlOption in the activeCheckBoxList like :
$base_id='my_custom_';
echo CHtml::activeCheckBoxList(
$parent,
'categoryIds',
Chtml::listData(Post::model()->getChilds($parent->id),'id','nome'),
array('id'=>$base_id.$parent->id);
By reading checkBoxList source code, I’m not sure that it is possible …
thank you luc for the reply…i tried but the html now its
<input type="hidden" name="Category[categoryIds]" value="" id="ytmy_custom_5">
<span id="Category_categoryIds"><input type="checkbox" name="Category[categoryIds][]" value="1" id="Category_categoryIds_0">
<label for="Category_categoryIds_0">Name 1</label>
<input type="checkbox" name="Category[categoryIds][]" value="2" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Name 2</label>
<input type="checkbox" name="Category[categoryIds][]" value="3" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Name 3</label>
<input type="checkbox" name="Category[categoryIds][]" value="4" id="Category_categoryIds_3"> <label for="Category_categoryIds_3">Name 4</label></span>
<input type="hidden" name="Category[categoryIds]" value="" id="ytmy_custom_6">
<span id="Category_categoryIds"><input type="checkbox" name="Category[categoryIds][]" value="7" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Name 7</label>
<input type="checkbox" name="Category[categoryIds][]" value="8" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Name 8</label>
<input type="checkbox" name="Category[categoryIds][]" value="9" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Name 9</label>
</span>
and doesnt work…if i select the check box of the first group (ytmy_custom_5)
i get this message…
Invalid argument supplied for foreach()
if($model->save())
foreach ($_POST['Category']['categoryIds'] as $categoryId) {
$postCategory = new PostCategory;
$postCategory->post_id = $model->id;
$postCategory->category_id = $categoryId;
if (!$postCategory->save()) print_r($postCategory->errors);
}
luc
(Luc Pfister)
November 14, 2012, 10:38pm
14
it’s strange. Ithought that my trick will give a custom id to the onput of type=“checkbox” and not on the hiddenfield …
activeCheckBoxList html options should apply to each checkbox input as sayd the documentation
oflodor
(Rdemattei)
November 15, 2012, 6:21am
15
luc:
it’s strange. Ithought that my trick will give a custom id to the onput of type=“checkbox” and not on the hiddenfield …
activeCheckBoxList html options should apply to each checkbox input as sayd the documentation
hi luc thanks…it works but change this value ytmy_custom_5 ytmy_custom_6
and no the checkbox inside…yes strange…