Yii is the best but I'm have two models , book and author . I need to process two forms for each one in a single view , as I do this in yii2.0 .
<?=
$this->render('_formBook', array('model' => $model));
?>
<?=
$this->render('_formAuthor', array('model' => $model));
?>
try this way
In your Controller
$modelBook = Book::findOne($id);
$modelAuthor = Author::findOne($key);
return $this->render('_formWithTheTwoForm',
'modelBook' => $modelBook,
'modelAuthor' => $modelAuthor,]);
Then in your view you can use both the model
Related
Tell me how to make the phone output in a readable way?
It is stored in the database as 1234567890, but you need to display the user - (123) 456-78-90.
I do not want to make a garden, obvio,usly there are already ready solutions.
In Controller
public function actionShowPhone()
{
$phone = "1234567890";
return $this->render('show-phone', ['phone' => $phone,]);
}
In View show-phone.php
<?= Html::encode($phone) ?>
Formatting phone numbers in Forms
If you are looking to format the phone number inside the ActiveForm you can use the \yii\widgets\MaskInput in the following way
<?=
$form->field($model, 'landline_phone')->widget(\yii\widgets\MaskedInput::className(), [
'mask' => '(999)-999-99-99'
]);
?>
or without ActiveForm
echo \yii\widgets\MaskedInput::widget([
'name' => 'phone',
'mask' => '(999)-999-99-99',
]);
Note: when saving the phone field you must save it as a number only in the database like 1234567890 so before you save you can use $this->landline_phone= preg_replace('/[^0-9]+/', '', $this->landline_phone); inside the beforeSave().
Formatting Phone Numbers as Text
Extending the \yii\i18n\Formatter
But if you want to print the phone number as text in the above format then a good way is to extend the yii\i18n\Formatter and create a custom component/helper in lets say common\components\ or app\components\ with the following code.
Note : change the namespace for the class accordingly
<?php
namespace common\components;
use yii\i18n\Formatter;
class FormatterHelper extends Formatter {
public function asPhone($value) {
return preg_replace("/^(\d{3})(\d{3})(\d{2})(\d{2})$/", "($1)-$2-$3-$4", $value);
}
}
and then in the common\config\main.php or app\config\web.php add the following under components.
'formatter' => [
'class' => '\common\components\FormatterHelper',
'locale' => 'en-US',
'dateFormat' => 'yyyy-MM-dd',
'datetimeFormat' => 'yyyy-MM-dd HH:mm:ss',
'decimalSeparator' => '.',
'thousandSeparator' => ',',
'currencyCode' => 'USD'
],
then you can use it like below
echo Yii::$app->formatter->asPhone('123456789')
and it will output the following as text
(123)-456-78-90
Using \yii\widgets\MaskedInputAssets
Another simplest and easiest way is to register the available MaskedInputAssets that uses RobinHerbots/Inputmask bundled and use javascript to mask the text
<?php
\yii\widgets\MaskedInputAsset::register($this);
$js = <<<SCRIPT
var selector = document.getElementById("mask");
var im = new Inputmask("(999)-999-99-99");
im.mask(selector);
SCRIPT;
// Register tooltip/popover initialization javascript
$this->registerJs ( $js , \yii\web\View::POS_READY);
?>
<div id="mask">
1234567890
</div>
I have a dataprovider to search a world but my listview does not display any record? How can i send this query object to listview?
every thing in my webpage is worked very good but at the output my list view show "not found any result" this mean my list view code is no have problem . problem is in my dataprovider and this query beacuse i customize that
my controller:
$query = new Query();
$dataProvidersearch=new ActiveDataProvider([
'query'=>$query->from('tbl_post')->Where(['like', 'title', $search])-
>andWhere(['like', 'en_title', $search])->andWhere(['like', 'content', $search])->andWhere(['like', 'en_content', $search]),
]);
this is my list view in my view:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$posts,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
]);
I'm not sure you have enough code here for someone to help. Even something simple like a listview could consist of a view, a controller, and two model files and your code could be failing at any of these points. You may have simply forgot to include the listview library at the top of your view, but we can't see that in your current example.
What I would recommend is using Gii to generate a listview. It is simple to do and once you have it created, you can study the code to see where you went wrong. You can see how to get started generating code with Gii here: http://www.yiiframework.com/doc-2.0/guide-start-gii.html
ANSWER FROM COMMENTS: Replace andWhere with orWhere, no results are found because no record can match 'title' and 'en_title' and 'content' and 'en_content'.
You are submitting $posts as 'dataProvider' while it should be dataProvidersearch
Instead of:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$posts,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
];
Should be:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$dataProvidersearch,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
];
i am using a dropdown list and i wants to know is there any way i can get the value selected by user instead of id.
I am using yii2 activeform with ArrayHelper map to populate the options which is like below
<?= $form->field($model, 'carname')->dropDownList(
ArrayHelper::map(Cars::find()->all(), 'id', 'name'),
['prompt' => 'Select Car Name']
) ?>
In controller on form submit it returns id which is good but i want to save the name directly in the database.
Thank you
<?= $form->field($model, 'carname')->dropDownList(
ArrayHelper::map(Cars::find()->all(), 'name', 'name'),
['prompt' => 'Select Car Name']
) ?>
Yii is the best but I'm having a wee bit of trouble replicating some of the code from yii1 to yii2, below I use the code in yii to render two models in one view and would like to replicate this using yii2. Grateful for any help. thanks
VwContractDetailsController
public function actionView($id)//create new dataprovider and pass param from url
{
$events=$dataProvider=new CActiveDataProvider('VwContractEvents', array(
'criteria'=>array(
'condition'=>'Contractkey_id=:aid',
'params'=>array(':aid'=>$id)
),
'pagination'=>array(
'pageSize'=>2
),
'sort' => array(
'defaultOrder' => 'EventDate DESC',
),
));
$this->render('view',array(
'model'=>$this->loadModel($id),
'events'=>$events,
));
}
vwContractEvents Index //provide full path for itemview
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'/vwcontractevents/_view',
'enablePagination' => true,
)); ?>
vwContractDetails view //render VwcontractEvents/index on vwContractDetails view
<?php
$this->renderPartial('/VwContractEvents/index',array('dataProvider'=>$events));
?>
The equivalent of renderPartial() in yii2 view is:
yii\base\View::render()
In controller you render with:
yii\base\Controller::render()
With links to each user.
For example. "SELECT * FROM users WHERE rating > 50" this query yields 120 results... how to print those results on a page in order of rating with links to each profile..
a leaderboard if you will
A good place to start would be to review the docs at http://book.cakephp.org/ but in short it will follow Cake's MVC principles. You'll need a model to interact with the database and pass this data back to a controller. The controller will then pass that information to the relevant view script and which point you can layout the recordset as you wish.
First, you should read up on CakePHP like #simnom has suggested. Once you do, your query and view code should look something like this:
Users Controller:
$users = $this->User->find('all', array('order'=> array('User.rating' => 'desc'), 'conditions'=>array('User.rating >' => '50')));
$this->set('users', $users);
View Code:
<?php
foreach ($users as $user):
echo $this->Html->link("View User", array('controller' => 'users', 'action' => 'view', $user['id']));
endforeach;
?>