Giix Crud

Hi lilli,

Happy new year!

No, giix doesn’t provide this feature, but you can just follow this wiki: How to upload a file using a model.

Are you making great progress?

Happy new Y(ii)ear to you! :D

I’ve made some progress but not that much (you know, holidays…).

Anyway, Yii it’s really great. I’m wondering how the 2.0 version will be… Let’s see.

P.S.: i’ve also found this tutorial about file uploading and it could be interesting.

Lol :)

It will be awesome. Check here for mouth-watering info.

That’s a good tutorial, with lots of details. It seems to cover the same approach used on the wiki.

Oh, and a security note: as a general rule, you shouldn’t save the uploaded file under a Web-accessible directory unless necessary and until you verify the nature of the file (that the file is actually an image, for example).

References:

https://www.owasp.org/index.php/Unrestricted_File_Upload

http://en.wikibooks.org/wiki/Web_Application_Security_Guide/File_upload_vulnerabilities

very useful links. thanks!

Hi Rodrigo,

me again. :rolleyes:

What if I need to add an extra data to the pivot tables?

Example:

STUDENTS

id_student

username

EXAMS

id_exam

exams

STUDENTS_X_EXAMS

id_student

id_exam

vote (this is the extra-primary keys data)

date (another one…)

How can I insert the additional datas on the Insert/Update views?

Thanks,

Lilli

[color="#006400"]/* moved from General Discussion */[/color]

I’ve got a Category model, an Image model and pivot table "category_image’ with additional field “position” for sorting images within a category. In Category controller:




	public function actionSort($catId)

	{

		foreach( $_POST['img'] as $order => $id )

		{

			$command = Yii::app()->content->createCommand(); //content is my DB connection

			$command->update(

				'category_image', 

				array(

					'position'=>$order,

				), 

				'image_id=:ID AND category_id=:catID', 

				array(':ID'=>$id,'catID'=>$catId)

			);

			//echo $id.'=>'.$order.'=>'.$catId.'/';

		}

		

	}



Hope this helps

Thanks luc,

but when i have to call your function?





I mean let's go back to my example:


in Students controller i have this create action, generated with giix.

	public function actionCreate() {

		$model = new Students;


		$this->performAjaxValidation($model, 'Students-form');


		if (isset($_POST['Students'])) {

			$model->setAttributes($_POST['Students']);

			$relatedData = array(

				'exams' => $_POST['Students']['exams'] === '' ? null : $_POST['Students']['exams'],

				);


			if ($model->saveWithRelated($relatedData)) {

				if (Yii::app()->getRequest()->getIsAjaxRequest())

					Yii::app()->end();

				else

					$this->redirect(array('view', 'id' => $model->id_student));

			}

		}


		$this->render('create', array( 'model' => $model));

	}

and in my view i have this:

	<div class="relationscreate">

		<h2><?php echo GxHtml::encode($model->getRelationLabel('exams')); ?></h2>

		<?php echo $form->checkBoxList($model, 'exams', GxHtml::encodeEx(GxHtml::listDataEx(Exams::model()->findAllAttributes(null, true)), false, true)); ?>

	</div>

where do i have to put the vote and date, additional fields, to associate them with the relative checkboxlist exam?

I think that in your case, checkBoxList is not the right choice.

Using a loop through


$exams=Exams::model()->findAll();

and create for each exam relation a checkbox for setting the relation and all the neccessary additionnal fields (vote, date …).

Luc, i still have many doubts…

forget the additional fields for a moment. how can i get the same output of a checkBoxList related to a model without using it?

this is the output of the checkBoxList:


<input type="hidden" name="Students[exams]" value="" id="ytStudents_exams"><span id="Students_exams">

<input type="checkbox" name="Students[exams][]" checked="checked" value="1" id="Students_exams_0"><label for="Students_exams_0">History</label><br>

<input type="checkbox" name="Students[exams][]" checked="checked" value="2" id="Students_exams_1"> <label for="Students_exams_1">Math</label><br>

<input type="checkbox" name="Students[exams][]" value="3" id="Students_exams_2"> <label for="Students_exams_2">Geography</label><br>

<input type="checkbox" name="Students[exams][]" value="4" id="Students_exams_3"> <label for="Students_exams_3">Literature</label>

for managing the insert/update of a many to many relations i need something like a checkBoxList.


			$exams=Exams::model()->findAll();

			foreach ($exams as $key=>$value){

				//i really can't find a way for having the same output

//(i can get just the labels but not the value or if they're checked or not)

			}



i’m quite desperate… :blink:

ok,

if i create the array like this:


$exams=GxHtml::encodeEx(GxHtml::listDataEx(Exams::model()->findAllAttributes(null, true)), false, true);

i will have an array with as key the primary key of Exams and as value the value of the exams but it’s not enough.

i still have the problem in update action, because, without checkBoxList, i don’t know how to pass the model to say if it’s checked or not.

help, please. all my project depends if i can fix this problem or not.

probably this is not the best solution, but it’s a solution (and i don’t see many alternatives):

on students view:




$exams=GxHtml::encodeEx(GxHtml::listDataEx(Exams::model()->findAllAttributes(null, true)), false, true);


foreach ($exams as $key=>$value){

  echo "<input type='text' name='Students[vote][".$key."]' value='".$students_x_exams[$key]."' >";

}



and in the update action:




public function actionUpdate($id) {

	$model = $this->loadModel($id, 'Students');


	$students_x_exams = StudentsXExams::model()->findAll('id_student=:id_student',

			array(':id_student'=>(int)  $id));

	$students_x_exams=CHtml::listData($students_x_exams,"id_exam","vote");




	...


		if ($model->saveWithRelated($relatedData)) {


			//save votes

			$students_x_exams= new StudentsXExams();

			foreach ($_POST['Students']['vote'] as $id_exam=>$vote){

				$criteria= new CDbCriteria();

				$criteria->condition="id_student=".$model->id_student;

				$criteria->addCondition("id_exam=".$id_exam);

				$istr_x_agg->updateAll(array('vote'=>$vote),$criteria);

			}

			

			$this->redirect(array('view', 'id' => $model->id_student));

		}


	...


	$this->render('update', array(

			'model' => $model,

			'students_x_exams'=>$students_x_exams,

			));

}



Hi Guys,

I have a little problem. On my 1.1.12, I have absolutely no problem, but I’ve copied my models, controllers, and views to a 1.1.13 version, and I’ve got the following PHP warning at only by one single “model” or “module”:


htmlspecialchars(): Invalid multibyte sequence in argument


...\framework\web\helpers\CHtml.php(98)


return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);

Stack Trace:


#0...yii\framework\web\helpers\CHtml.php(98): htmlspecialchars()

#1...yii\framework\web\helpers\CHtml.php(2071): CHtml::encode()

#2...yii\framework\web\helpers\CHtml.php(1566): CHtml::listOptions()

#3...yii\framework\zii\widgets\grid\CDataColumn.php(95): CHtml::activeDropDownList()

#4...yii\framework\zii\widgets\grid\CGridColumn.php(109): CDataColumn->renderFilterCellContent()

#5...yii\framework\zii\widgets\grid\CGridView.php(505): CGridColumn->renderFilterCell()

#6...yii\framework\zii\widgets\grid\CGridView.php(483): CGridView->renderFilter()

#7...yii\framework\zii\widgets\grid\CGridView.php(453): CGridView->renderTableHeader()

#8...yii\framework\zii\widgets\CBaseListView.php(160): CGridView->renderItems()

#9 unknown(0): CBaseListView->renderSection(array("{items}", "items"))

#10...yii\framework\zii\widgets\CBaseListView.php(143): preg_replace_callback("/{(\w+)}/", array(CGridView, "renderSection"), "{summary} {items} {pager}")

#11...yii\framework\zii\widgets\CBaseListView.php(128): CBaseListView->renderContent()

#12...yii\framework\web\CBaseController.php(173): CBaseListView->run()

#13...(project folder)\protected\views\munkakor\admin.php(80): CBaseController->widget("zii.widgets.grid.CGridView", array("id" => "munkakor-grid", "dataProvider" => CActiveDataProvider, "filter" => Munkakor, "columns" => array("id", array("name" => "nbsp_name", "type" => "raw", "htmlOptions" => array("style" => "white-space: nowrap")), "rov", "root", ...)))

#14...yii\framework\web\CBaseController.php(126): require("C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdo...")

#15...yii\framework\web\CBaseController.php(95): CBaseController->renderInternal("C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdo...", array("model" => Munkakor), true)

#16...yii\framework\web\CController.php(869): CBaseController->renderFile("C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdo...", array("model" => Munkakor), true)

#17...yii\framework\web\CController.php(782): CController->renderPartial("admin", array("model" => Munkakor), true)

#18...(project folder)\protected\controllers\MunkakorController.php(90): CController->render("admin", array("model" => Munkakor)) 

#19...yii\framework\web\actions\CInlineAction.php(49): MunkakorController->actionAdmin()

#20...yii\framework\web\CController.php(308): CInlineAction->runWithParams(array())

#21...yii\framework\web\CController.php(286): CController->runAction(CInlineAction)

#22...yii\framework\web\CController.php(265): CController->runActionWithFilters(CInlineAction, array())

#23...yii\framework\web\CWebApplication.php(282): CController->run("admin")

#24...yii\framework\web\CWebApplication.php(141): CWebApplication->runController("munkakor/admin")

#25...yii\framework\base\CApplication.php(169): CWebApplication->processRequest()

#26...(project folder)\index.php(13): CApplication->run()

after a few hours of experimenting with utf8 settings (it seems all good to me everywhere) and googling I could figure out that the problem is with this one single line in protected\views\munkakor\admin.php’s CGridView’s columns section:


array(

            'name' => 'uzem_id',

            'value' => 'GxHtml::link($data->uzem, array("uzem/view", "id" => $data->uzem_id))',

            'type' => 'raw',

            'filter' => GxHtml::listDataEx(Uzem::model()->findAll(array('order' => 'root, lft ASC')), 'id', 'Nbsp_name'),

        ),

more specifically:

‘filter’ => GxHtml::listDataEx(Uzem::model()->findAll(array(‘order’ => ‘root, lft ASC’)), ‘id’, ‘Nbsp_name’),

if I comment this out, the warning goes away. there are absolutely no special characters anywhere, no BOMs…

What can be wrong with this code?

UPDATE: the problem is definitely with a function in my model:




public function getNbspName() {

        return str_repeat("&nbsp;", ($this->level - 1) * 4) . $this->name;

    }



it doesn’t like &nbsp; there, but so far I had no problem with this. It’s only since of the newer version of yii…

Thanks a lot!

BR

c