I don't want to allow guest phones/new. They should have access for phones/index and one other thing only. But guests were able to access all actions of Phones controller. I need your help to find out the mistake I did.
Here is the ACL plugin
<?php
use Phalcon\Acl;
use Phalcon\Acl\Role;
use Phalcon\Acl\Resource;
use Phalcon\Events\Event;
use Phalcon\Mvc\User\Plugin;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Acl\Adapter\Memory as AclList;
/**
* SecurityPlugin
*
* This is the security plugin which controls that users only have access to the modules they're assigned to
*/
class SecurityPlugin extends Plugin
{
/**
* Returns an existing or new access control list
*
* #returns AclList
*/
public function getAcl()
{
if (!isset($this->persistent->acl)) {
$acl = new AclList();
$acl->setDefaultAction(Acl::DENY);
//Register roles
$roles = array(
'admin' => new Role('Admin'),
'editor' => new Role('Editor'),
'guests' => new Role('Guests')
);
foreach ($roles as $role) {
$acl->addRole($role);
}
//Admin area resources
$adminResources = array(
'dashboard' => array('index'),
'phones' => array('index', 'all', 'new', 'edit', 'save', 'create', 'delete', 'search'),
'users' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete', 'saveProfile', 'profile'),
);
foreach ($adminResources as $resource => $actions) {
$acl->addResource(new Resource($resource), $actions);
}
//Editor area resources
$editorResources = array(
'dashboard' => array('index'),
'phones' => array('index', 'all', 'new', 'edit', 'save', 'create', 'search'),
'users' => array('saveProfile', 'profile'),
);
foreach ($editorResources as $resource => $actions) {
$acl->addResource(new Resource($resource), $actions);
}
//Public area resources
$publicResources = array(
'index' => array('index'),
'about' => array('index'),
'login' => array('index', 'check', 'logout'),
'errors' => array('show404', 'show500'),
'contact' => array('index', 'send'),
'phones' => array('index', 'search'),
);
foreach ($publicResources as $resource => $actions) {
$acl->addResource(new Resource($resource), $actions);
}
//Grant access to public areas to both users and guests
foreach ($roles as $role) {
foreach ($publicResources as $resource => $actions) {
$acl->allow($role->getName(), $resource, '*');
}
}
//Grant access to private area to role Admin
foreach ($adminResources as $resource => $actions) {
foreach ($actions as $action) {
$acl->allow('Admin', $resource, $action);
}
}
//Grant access to private area to role Admin
foreach ($editorResources as $resource => $actions) {
foreach ($actions as $action) {
$acl->allow('Editor', $resource, $action);
}
}
//The acl is stored in session, APC would be useful here too
$this->persistent->acl = $acl;
}
return $this->persistent->acl;
}
/*
* This action is executed before execute any action in the application
*
* #param Event $event
* #param Dispatcher $dispatcher
*/
public function beforeDispatch(Event $event, Dispatcher $dispatcher)
{
$auth = $this->session->get('auth');
if (!$auth) {
$role = 'Guests';
} else {
switch ($auth['role']) {
case 1:
$role = "Admin";
break;
case 2:
$role = "Editor";
break;
default:
$role = "Guests";
break;
}
}
$controller = $dispatcher->getControllerName();
$action = $dispatcher->getActionName();
$acl = $this->getAcl();
$allowed = $acl->isAllowed($role, $controller, $action);
if ($allowed != Acl::ALLOW) {
$dispatcher->forward(array(
'controller' => 'errors',
'action' => 'show401'
));
return false;
}
}
}
I fixed the issue by changing wildcard to specific action. I actually copied code from invo and overlooked the thing.
//Grant access to public areas to both users and guests
foreach ($roles as $role) {
foreach ($publicResources as $resource => $actions) {
$acl->allow($role->getName(), $resource, $actions);
}
}
Related
I'm developing a basic crud application using PHP Codeigniter 3 with MySQL database.
I have done add and list method successfully but now I want to edit the record and have included an edit button in the records table in view. when I click on the button it will go to my edit record view but it shows some errors. I checked everything but I fail to resolve it.
This is my controller
<?php
class User extends CI_controller
{
public function index()
{
$this->load->model('User_model');
$users = $this->User_model->getUsers();
// print_r($users);
$data = array();
$data['users'] = $users;
$this->load->view('users/list',$data);
}
public function create()
{
//load model here
$this->load->model('User_model');
//load library
$this->load->library('form_validation');
$this->load->library('ckeditor');
$this->load->library('ckfinder');
//set rules
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('projectname', 'Projectname', 'required');
$this->form_validation->set_rules('projecttype', 'Projecttype', 'required');
$this->form_validation->set_rules('phone', 'Phone', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('address', 'Address', 'required');
$this->form_validation->set_rules('projectdescription', 'Projectdescription', 'required');
// $this->form_validation->set_rules('termname', 'Termname', 'required');
// $this->form_validation->set_rules('termdescription', 'Termdescription', 'required');
$this->form_validation->set_rules('article_description', 'Article_description', 'required');
if ($this->form_validation->run() == true) {
// print_r($_POST);
// Array ( [name] => muhammad zawish [projectname] => test [projecttype] => Hardware [phone] => 03206270391 [email] => muhammadzawish#gmail.com [address] => Gondal Road, Sialkot, Punjab, Pakistan [projectdescription] => aaaaaaaaaa [termname] => aaaaaa [termdescription] => aaaaaaaa )
$name = $this->input->post('name');
$projectname = $this->input->post('projectname');
$projecttype = $this->input->post('projecttype');
$phone = $this->input->post('phone');
$email = $this->input->post('email');
$address = $this->input->post('address');
$projectdescription = $this->input->post('projectdescription');
// $termname = $this->input->post('termname');
// $termdescription = $this->input->post('termdescription');
$article_description = $this->input->post('article_description');
// $formData = array('name' => $name, 'projectname' => $projectname, 'projecttype' => $projecttype, 'phone' => $phone, 'email' => $email, 'address' => $address, 'projectdescription' => $projectdescription, 'termname' => $termname, 'termdescription' => $termdescription,);
$formData = array('name' => $name, 'projectname' => $projectname, 'projecttype' => $projecttype, 'phone' => $phone, 'email' => $email, 'address' => $address, 'projectdescription' => $projectdescription, 'article_description' => $article_description);
//ck editor files
$this->ckeditor->basePath = base_url() . 'asset/ckeditor/';
$this->ckeditor->config['toolbar'] = array(
array('Source', '-', 'Bold', 'Italic', 'Underline', '-', 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo', '-', 'NumberedList', 'BulletedList')
);
$this->ckeditor->config['language'] = 'it';
$this->ckeditor->config['width'] = '730px';
$this->ckeditor->config['height'] = '300px';
//Add Ckfinder to Ckeditor
$this->ckfinder->SetupCKEditor($this->ckeditor, '../../asset/ckfinder/');
$this->User_model->add_user($formData);
$this->session->set_flashdata('message', 'Record has been added successfully');
redirect(base_url('user/index'));
} else {
$this->load->view('users/create');
}
}
function edit($id){
$this->load->model('User_model');
$row = $this->User_model->getUser($id);
$data1 = array();
$data1['row'] = $row;
$this->load->view('users/edit', $data1);
}
}
this is my model
<?php
class User_model extends CI_Model{
public function add_user($formArray){
// $this->load->library('session');
$this->db->insert('users', $formArray);
}
//for view data fetch from database
public function getUsers(){
$users = $this->db->get('users')->result_array();
return $users;
}
//for edit record
function getUser($id){
$this->db->where('id', $id);
$row = $this->db->get('users')->row_array();
return $row;
}
}
?>
when I access my edit.php view
404 Page Not Found
The page you requested was not found.
I am trying to add captcha validation based on scenario, for which I am first retrieving number of failed attempts from database. For which I am using checkattempts() function. Based on the result I am displaying captcha in view and adding scenario condition in controller as below.
In LoginForm model:
public function rules()
{
return [
[['username', 'password'], 'required', 'on'=>'loginpage'],
[['username', 'password'], 'required', 'on'=>'withCaptcha'],
[['reference_url'], 'safe'],
[['verifyCode'], 'captcha', 'skipOnEmpty' => true,'on'=>'withCaptcha'],
['username','email', 'on'=>'loginpage', 'message'=> 'Please enter a valid email address'],
['password', 'validatePassword', 'on'=>'loginpage'],
['password', 'validatePassword', 'on'=>'withCaptcha'],
];
}
public function checkattempts($uname)
{
$user = \frontend\models\User::findByEmail($uname);
$ip = $this->get_client_ip();
if($user){
$data = (new Query())->select('*')
->from('login_attempts')
->where(['ip' => $ip])->andWhere(['user_ref_id' => $user->id])
->one();
if($data["attempts"] >=3){
return true;
}else{
return false;
}
}
return false;
}
in SiteController.php controller
public function actionLogin() {
if (!\Yii::$app->user->isGuest) {
return $this->redirect(Yii::$app->getUrlManager()->getBaseUrl() . '/../../');
}
$model = new \common\models\LoginForm();
$model->scenario = 'loginpage';
$captcha = false;
if(Yii::$app->request->post()){
$post_variables =Yii::$app->request->post('LoginForm');
if ($model->checkattempts($post_variables['username'])) {
$model->scenario = 'withCaptcha';
$captcha = true;
}
}
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->login(); print_r($model->getErrors()); exit;
} else {
return $this->render('login', [
'model' => $model, 'captcha' => $captcha,
]);
}
In my login.php view:
<?php if($captcha) { ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(),
['template' => '<div class="captcha_img">{image}</div>'
. '<a class="refreshcaptcha" href="#">'
. Html::img('/images/imageName.png',[]).'</a>'
. 'Verification Code{input}',
])->label(FALSE); ?>
<?php } ?>
In my controller when I am tring to print model errors at $model->login() function it is giving below error everytime even though the verification code is correct.
Array ( [verifycode] => Array ( [0] => The verification code is incorrect. ) )
Why is it failing every time. Is there any mistake in the code written?
Thanks in advance
I'm trying to upload multiple images when I create a Post, but only one Image is stored in database, in server all images are saved but in my table only first image is saved but not all so I created function for upload
// Function to upload images
public function CreateGallery($model_Post, $model_Gallery)
{
$model_Post->Post_id = md5(microtime());
$model_Post->User_id = Yii::$app->user->id;
$GLRID = md5(uniqid());
// file
$GFolder = 'upload/images/'. $model_Post->Post_id .'/' ;
mkdir ($GFolder, 0777, true);
if ( $model_Post->save() ){
$model_Gallery->GalleryFile = UploadedFile::getInstances($model_Gallery, 'GalleryFile');
$ly_GalName = uniqid();
foreach ($model_Gallery->GalleryFile as $GalleryImage) {
++$ly_GalName;
$model_Gallery->Gallery_id = ++$GLRID;
$model_Gallery->User_id = $model_Post->User_id;
$model_Gallery->Post_id = $model_Post->Post_id;
$GalleryImage->saveAs($GFolder . $GalName . '.' . $GalleryImage->extension);
$model_Gallery->Gallery_image = $GFolder . $GalName . '.' . $GalleryImage->extension;
}
$model_Gallery->save(false);
}
}
and inside my controller I add the function I created
public function actionCreate()
{
$model_Post = new Post();
$model_Gallery = new Gallery;
$actions_UploadImages = new ActionsUploadImages();
if ($model->load(Yii::$app->request->post()) ) {
$actions_UploadImages->CreateGallery($model_Post, $model_Gallery)
return $this->redirect(['view', 'id' => $model_Post->Post_id]);
} else {
return $this->render('create', [
'model_Post' => $model_Post,
'model_Gallery' => $model_Gallery,
]);
}
}
and my views/create my code is
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model_Post, 'Post_title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model_Gallery, 'GalleryFile[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
<?= $form->field($model_Post, 'Post_text')->textarea(['rows' => 2]) ?>
<?= $form->field($model_Post, 'Permission_id')->dropdownList($model_Permission->PermissionList()) ?>
<div class="form-group">
<?= Html::submitButton($model_Post->isNewRecord ? 'Create' : 'Update', ['class' => $model_Post->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
I tried to change gallery_id to AUTO_INCREMENT but still the same problem, I tried to remove if ( $model_Post->save() ), but I get error from database. Only like it's is now it's save all images on server but in table save only one image.
//// gallery model :
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "gallery".
*
* #property string $Gallery_id
* #property string $Gallery_image
* #property string $Post_id
* #property string $User_id
*
* #property Post $post
* #property Userlogin $user
*/
class Gallery extends \yii\db\ActiveRecord
{
public $GalleryFile;
/**
* #inheritdoc
*/
public static function tableName()
{
return 'gallery';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['Gallery_id', 'Post_id', 'User_id'], 'required'],
[['Gallery_id'], 'string', 'max' => 200],
[['Gallery_image'], 'string', 'max' => 350],
[['Post_id', 'User_id'], 'string', 'max' => 300],
[['GalleryFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg, gif', 'maxFiles' => 5],
[['Post_id'], 'exist', 'skipOnError' => true, 'targetClass' => Post::className(), 'targetAttribute' => ['Post_id' => 'Post_id']],
[['User_id'], 'exist', 'skipOnError' => true, 'targetClass' => Userlogin::className(), 'targetAttribute' => ['User_id' => 'User_id']],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'Gallery_id' => 'Gallery ID',
'Gallery_image' => 'Gallery Image',
'Post_id' => 'Post ID',
'User_id' => 'User ID',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getPost()
{
return $this->hasOne(Post::className(), ['Post_id' => 'Post_id']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(Userlogin::className(), ['User_id' => 'User_id']);
}
/**
* #inheritdoc
* #return \app\queries\GalleryQuery the active query used by this AR class.
*/
public static function find()
{
return new \app\queries\GalleryQuery(get_called_class());
}
}
you need write a new model en the for each , like that
Function
Create a new model object in the for each .
Set the $model_Gallery->save(false); in inside the foreach
// Function to upload images
public function CreateGallery($model_Post, $model_Gallery)
{
$model_Post->Post_id = md5(microtime());
$model_Post->User_id = Yii::$app->user->id;
$GLRID = md5(uniqid());
// file
$GFolder = 'upload/images/' . $model_Post->Post_id . '/';
mkdir($GFolder, 0777, true);
if ($model_Post->save()) {
$model_Gallery->GalleryFile = UploadedFile::getInstances($model_Gallery, 'GalleryFile');
$ly_GalName = uniqid();
foreach ($model_Gallery->GalleryFile as $GalleryImage) {
$model_GalleryImage = new Gallery;
++$ly_GalName;
$model_GalleryImage->Gallery_id = ++$GLRID;
$model_GalleryImage->User_id = $model_Post->User_id;
$model_GalleryImage->Post_id = $model_Post->Post_id;
$GalleryImage->saveAs($GFolder . $GalName . '.' . $GalleryImage->extension);
$model_GalleryImage->Gallery_image = $GFolder . $GalName . '.' . $GalleryImage->extension;
$model_GalleryImage->save(false); //here , remove 'false' to work validations
}
}
}
Controller
when the action load the data ,it should not be like that ?
if ($model_Gallery->load(Yii::$app->request->post()) and $model_Post->load(Yii::$app->request->post())) {
//the rest of the code
}
Please note that we use save(false) to skip over validations inside the models as the user input data... with the user input
All you have to do is iterate $_FILES object using foreach loop and create new model every time. To validate code manage flag for that
In an ActiveDataProvider you can use closures as values, like:
$dataprovider = new ArrayDataProvider([
'allModels' => $array
]);
$gridColumns = [
'attrib_1',
[
'attribute' => 'attrib_2',
'label' => 'Label_2',
'value' => function($model) {
return Html::encode($model->value_2);
}
],
'attrib_3'
];
echo GridView::widget([
'dataProvider'=> $dataprovider,
'columns' => $gridColumns
]);
Is it possible to do the same or something like this, in an ArrayDataProvider?
Yes. Only difference is that $model is not an object but array so:
'value' => function($model) {
return Html::encode($model['value_2']);
}
For this purpose, I have created an extended version of ActiveDataProvider, that for each model got from provider I call a callback.
This is the custom ActiveDataProvider, put in common\components namespace in this case.
<?php
namespace common\components;
class CustomActiveDataProvider extends \yii\data\ActiveDataProvider
{
public $formatModelOutput = null;
public function getModels()
{
$inputModels = parent::getModels();
$outputModels = [];
if($this->formatModelOutput != null)
{
for($k=0;$k<count($inputModels);$k++)
{
$outputModels[] = call_user_func( $this->formatModelOutput, $k , $inputModels[$k]);
}
}
else
{
$outputModels = $inputModels;
}
return $outputModels;
}
}
This is the action in controller that uses it. For reusability, I call a model method instead calling a clousure, but you can call also a clousure.
public function actionIndex()
{
$query = Model::find();
$dataProvider = new \common\components\CustomActiveDataProvider([
'query' => $query,
'pagination' => ['pageSize' => null],
'formatModelOutput' => function($id, $model) {
return $model->dataModelPerActiveProvider;
}
]);
return $dataProvider;
}
At last, this is the method getDataModelPerActiveProvider in model:
public function getDataModelPerActiveProvider()
{
$this->id = 1;
// here you can customize other fields
// OR you can also return a custom array, for example:
// return ['field1' => 'test', 'field2' => 'foo', 'field3' => $this->id];
return $this;
}
Getting error when I am trying to create dynamic form in using yii2-dynamicform. at the time of create method it is working fine but at the time of update showing the error. I have two tables one is
1.vendors &
2.vendors_more_categories
Relation is 1-* between vendors & vendors_more_categories I just refereed https://github.com/wbraganca/yii2-dynamicform this link.
<?php
namespace app\controllers;
namespace backend\controllers;
use Yii;
use app\models\Vendors;
use app\models\VendorsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;
use app\models\VendorsMoreCategories;
use backend\models\Model;
use yii\web\Response;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
/**
* VendorsController implements the CRUD actions for Vendors model.
*/
class VendorsController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['index','create', 'update', 'delete'],
'rules' => [
[
'actions' => ['index','create', 'update', 'delete'],
'allow' => true,
'roles' => ['#'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Vendors models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new VendorsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Vendors model.
* #param integer $id
* #return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Vendors model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Vendors();
$modelsVendorsMoreCategories = [new VendorsMoreCategories];
if($model->load(Yii::$app->request->post())){
$modelsVendorsMoreCategories = Model::createMultiple(VendorsMoreCategories::classname());
Model::loadMultiple($modelsVendorsMoreCategories, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsVendorsMoreCategories) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsVendorsMoreCategories as $modelVendorsMoreCategories) {
$modelVendorsMoreCategories->vmc_ven_id = $model->ven_id;
if (! ($flag = $modelVendorsMoreCategories->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
$model->file = UploadedFile::getInstance($model, 'file');
$save_file = '';
if($model->file){
$imagename = Vendors::find()->orderBy('ven_id DESC')->one();
$imagename=$imagename->ven_id+1;
$imagepath = 'images/imgvendors/'; // Create folder under web/uploads/logo
$model->ven_business_logo = $imagepath.$imagename.'.'.$model->file->extension;
$save_file = 1;
}
if ($model->save(false)) {
if($save_file){
$model->file->saveAs($model->ven_business_logo);
}
return $this->redirect(['view', 'id' => $model->ven_id]);
}
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}else {
return $this->render('create', [
'model' => $model,
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
}
}
/**
* Updates an existing Vendors model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
//print_r($model->attributes);
$modelsVendorsMoreCategories = $model->ven_id;
if($model->load(Yii::$app->request->post())){
$oldIDs = ArrayHelper::map($modelsVendorsMoreCategories, 'id', 'id');
$modelsVendorsMoreCategories = Model::createMultiple(VendorsMoreCategories::classname(), $modelsVendorsMoreCategories);
Model::loadMultiple($modelsVendorsMoreCategories, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsVendorsMoreCategories, 'id', 'id')));
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsVendorsMoreCategories) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (! empty($deletedIDs)) {
Address::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelsVendorsMoreCategories as $modelVendorsMoreCategories) {
$modelVendorsMoreCategories->vmc_ven_id = $model->ven_id;
if (! ($flag = $modelVendorsMoreCategories->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
$model->file = UploadedFile::getInstance($model, 'file');
$save_file = '';
if($model->file){
$imagepath = 'images/imgvendors/'; // Create folder under web/uploads/logo
$model->ven_business_logo = $imagepath.$model->ven_id.'.'.$model->file->extension;
$save_file = 1;
}
if ($model->save(false)) {
if($save_file){
$model->file->saveAs($model->ven_business_logo);
}
return $this->redirect(['view', 'id' => $model->ven_id]);
}
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}else {
return $this->render('update', [
'model' => $model,
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
}
}
/**
* Deletes an existing Vendors model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Vendors model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Vendors the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Vendors::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
//Function used for deleting the images
public function actionDeleteimg($id, $field)
{
$img = $this->findModel($id)->$field;
if($img){
if (!unlink($img)) {
return false;
}
}
$img = $this->findModel($id);
$img->$field = NULL;
$img->update();
return $this->redirect(['update', 'id' => $id]);
}
//Function used for getting more sub categories for vendor
public function actionGetSubCategories()
{
$mbcid=$_GET['ven_main_category_id'];
$sbcid=$_GET['ven_sub_category_id'];
echo $mbcid;
}
public function actionLists($id)
{
$countVendors = Vendors::find()->where(['ven_contact_person_id' => $id])->count();
$vendors = Vendors::find()->where(['ven_contact_person_id' => $id])->all();
if ($countVendors > 0) {
foreach ($vendors as $vendor) {
echo "<option value='" . $vendor->ven_id . "'>" . $vendor->ven_company_name . "</option>";
}
} else {
echo "<option></option>";
}
}
}
You accessing modelsVendorsMoreCategories[0] (as an element of an array )
'model'=> $modelsVendorsMoreCategories[0],
in your DinamicForm widget but when you update the model you pass as $modelsVendorsMoreCategories this value
$modelsVendorsMoreCategories = $model->ven_id;
(don't seems an array, you must be sure this object contain an array with a proper element in 0 index))
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ?
[new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
$modelsVendorsMoreCategories should contain the VendorsMoreCategories model in actionUpdate method. In this code ($modelsVendorsMoreCategories = $model->ven_id) $modelsVendorsMoreCategories contains $model->ven_id. It is an integer value, not the VendorsMoreCategories object.
The Vendors model should contain a relation on the VendorsMoreCategories model:
class Vendors extends \yii\db\ActiveRecord
{
....
public function getVendorsMoreCategories()
{
return $this->hasMany(VendorsMoreCategories::className(), 'vendor_id'=>'id']);
}
}
And then, you should use that relation in your actionUpdate method:
$model = $this->findModel($id);
$modelsVendorsMoreCategories = $model->vendorsMoreCategories;
if(!$modelsVendorsMoreCategories) {
$modelsVendorsMoreCategories = [new VendorsMoreCategories];
}
In your actionUpdate you have:
$modelsVendorsMoreCategories = $model->ven_id;
But you should have:
$modelsVendorsMoreCategories = $model->nameOfMyRelation;
To get what's the real name, go in your $model, and look for something like:
/**
* #return \yii\db\ActiveQuery
*/
public function getNameOfMyRelation()
{
return $this->hasMany(VendorsMoreCategories::className(), ['ven_id' => 'id']);
}
If you don't have any function making the relation of this two tables, write one. If you having trouble doing that, you can always use the gii's model generator and check the Vendors model (you dont need to replace it, just preview the code).
Check your create.php file in view folder, pass required variable on
_form.php file from here as:-
<?= $this->render('_form', [
'model' => $model,
'modelsAddress' => $modelsAddress,
]) ?>
Check Your create file in view folder:
Controller:
Controller pass the parameter into create.php
return $this->render('create', [
'model' => $model,
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
View:create.php
If You miss the parameter: 'modelsVendorsMoreCategories' =>$modelsVendorsMoreCategories.
It shows the Undefined variable error in _form.php page.
<?= $this->render('_form', [
'model' => $model,
'modelsVendorsMoreCategories' =>$modelsVendorsMoreCategories
])?>
View:_form.php
$modelsVendorsMoreCategories[0];
the paramater not passing before now it passing.