yii1.x

render a view from another controller, yii

别来无恙 提交于 2021-02-05 20:59:13
问题 The controller: controllers |-FooController.php |-BarController.php The views: view |-foo| | |-index.php | |-error.php | |-bar| |-index.php How to render the error.php view with an action of the bar controller? I have tried: $this->render('foo/error'); But it doesn't work. 回答1: try this $this->render('//foo/error'); 回答2: If you don't echo it , you will get a blank page. The correct way is <?= $this->render('//foo/error'); ?> or <?php echo $this->render('//foo/error'); ?> This also works for

How to bind array parameters in Yii framework?

天涯浪子 提交于 2019-12-21 04:12:49
问题 I have below code: $inputs = "1,2,3,4,5"; $sql = "SELECT * FROM obj WHERE id IN(:input)"; $commond = Yii::app()->db->createCommand($sql); $commond->bindValue(":input", $inputs , PDO::PARAM_STR); But the query result is incorrect. How to bind params for such IN condition? 回答1: for now use it like this $command = Yii::app()->db->createCommand() ->select() ->from('tableName') ->where(array('in', 'id', explode(',', $inputs))); I ll try to get back with $command->bindValue() method. 回答2: Having

yii: how to make a unique rule for two attributes

你说的曾经没有我的故事 提交于 2019-12-18 11:40:54
问题 I have a table like this: (id, name, version, text). (name, version) is unique key, how can i make a rule to validate this. 回答1: This can be done by Yii itself, you do not need an extension for it. However an extension can help cleaning up the rules() method as described here: http://www.yiiframework.com/extension/unique-attributes-validator/ This is the code (copied from that site) which will work without using the extension: public function rules() { return array( array('firstKey', 'unique'

3 column layout - how to have a limit in each column

十年热恋 提交于 2019-12-13 03:48:28
问题 I have a 3-column listing (data coming from api). Each page has a limit of 30. I've managed to put the listing into 3 columns. I'm having some problems with my code where when it's on a bigger screen, it is no longer a 3-column layout and each column has more than 10 data. Questions: 1) How do I make sure that this layout stays a 3-column layout even when it goes on a bigger screen? 2) How do I put a limit of 10 in each column? php/html: <div id="native" class="margin-top-2x"> <ul> <?php

Using transaction in a loop in Yii

被刻印的时光 ゝ 提交于 2019-12-12 11:16:37
问题 I have an array of active records and want to change some field of them with a loop in this manner: $error = false; foreach ($items as $item) { $item->is_paid = self::PENDING; $error = $error || !$item->save(); } return $error; What I want to do is to change the is_paid property for all of this items. If on fails, roll back the others. How can I use transaction to solve this problem? 回答1: By a brief look here, I was able to find the transaction management in yii, something like the following

Get base URL in Yii console application

ぐ巨炮叔叔 提交于 2019-12-08 17:35:47
问题 How to get base URL in a Yii CConsoleApplication application? I tried Yii::app()->request->getBaseUrl(true) and ended up with the following error. Undefined index: SERVER_NAME (/var/www/yii/framework/web/CHttpRequest.php:279) 回答1: There is no request object in a console application. the request object in a web application its an instance of CHttpRequest, if you are generating URLs in an offline task, you have to configure the baseUrl in some other way, perhaps in the configuration: "request"

Batch insert in Yii

☆樱花仙子☆ 提交于 2019-12-04 06:28:35
I need to insert multiple ActiveRecord object in Yii,if all of them inserted $transaction = Yii::app()->db->beginTransaction(); for ($i = 0;$i < 10;$i++){ $model = new Mymodel(); $model->x = $i; if (!$model->save()){ $transaction->rollback(); break; } } if ($transaction->active) $transaction->commit(); Now I need to insert all of them in one query,How can I do it during using active record? While not entirely Yii like, it can be made as an extension/component, and is treated like a normal command, so transactions still apply. It would be entirely possible to set this up to utilise parameters

How to bind array parameters in Yii framework?

那年仲夏 提交于 2019-12-03 13:05:07
I have below code: $inputs = "1,2,3,4,5"; $sql = "SELECT * FROM obj WHERE id IN(:input)"; $commond = Yii::app()->db->createCommand($sql); $commond->bindValue(":input", $inputs , PDO::PARAM_STR); But the query result is incorrect. How to bind params for such IN condition? for now use it like this $command = Yii::app()->db->createCommand() ->select() ->from('tableName') ->where(array('in', 'id', explode(',', $inputs))); I ll try to get back with $command->bindValue() method. Having come across this problem a few times in my projects I have come-up with the following Yii work-around using

Yii multi page form wizard best practice

纵饮孤独 提交于 2019-11-27 09:32:47
问题 I am trying to build a multi-page form with Yii, but am quite new to PHP and Yii and am wondering what the best practice is for writing a multi page form. So far, what I am planning to do is to add a hidden field named 'step' which contains the current step the user is on in the form (the form is broken into 3 steps/pages). So with that in mind, this is how I plan to handle the user clicking on previous/next buttons in the Controller: public function actionCreate() { $userModel = new User;