YII probably import limit issue / importing model from other module - php

I am working on a project in YII. I have planned to develop the site modules based. Everything was working fine but today I created a module and imported it the main module. now, when i go to the site at my localhost the browser displays the The connection was reset page. here is the code
Users Model in /modules/users/models/Users.php
public function relations()
{
Yii::import('application.modules.companies.models.Company');
Yii::import('application.modules.clients.models.Clients');
Yii::import('application.modules.events.models.Events');
Yii::import('application.modules.notifications.models.UserNotifications');
Yii::import('application.modules.tasks.models.UserTasks');
return array(
...
'contacts' => array(self::HAS_MANY, 'UserLeeds', 'user_id'),
'userNotifications' => array(self::HAS_MANY, 'UserNotifications', 'user_id'),
'userTasks' => array(self::HAS_MANY, 'UserTasks', 'user_id'),
'userRole' => array(self::BELONGS_TO, 'Roles', 'user_role'),
'userCompany' => array(self::BELONGS_TO, 'Company', 'user_company_id'),
'userClient' => array(self::BELONGS_TO, 'Clients', 'user_client_id'),
);
}
Notifications Model in /modules/notifications/models/UserNotifications.php
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'notificationType' => array(self::BELONGS_TO, 'Notifications', 'notification_type'),
'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
);
}
If in the Users Model I comment out the Yii::import('application.modules.notifications.models.UserNotifications'); line all works fine.
Any idea what I am missing here.
UPDATE
Well, just found that if I comment out any of the imported modules it works fine then..
does this mean YII limits the import of module/models to other module/modle to only 4?
Regards

Related

Order by related data doesn't work

I have model with relations:
public function relations()
{
return array(
'fidistr' => array(self::BELONGS_TO, 'Distributors', 'fidistr_id', 'order'=>'fsname'),
'fitown' => array(self::BELONGS_TO, 'Town', 'fitown_id'),
'distributorsPointMails' => array(self::HAS_MANY, 'DistributorsPointMail', 'fidistr_point_id'),
'distributorsPointPhones' => array(self::HAS_MANY, 'DistributorsPointPhones', 'fidistr_point_id'),
);
}
I would like to order data by "fsname" from related table. I tried this:
$models = DistributorsPoint::model()->findAll('fitown_id=:id', array('id' => $_POST['city_id']));
but it still returns unsorted data. Help please.
The order on the relation is only used when lazy loading the relation. From the Yii guide on relations:
Note: when using eager loading such relation options as 'order',
'group', 'having', 'limit' and 'offset' will be ignored. You should
setup such parameters at the main model criteria level if you wish
them to be applied.
You can pass a criteria array instead of a string as the first parameter of findAll(). This will be used to initialize a CDbCriteria object. To order by a relation you need to eager load the relation using with():
$models = DistributorsPoint::model()
->with('fidistr')
->findAll(array(
'condition' => 'fitown_id=:id',
'order' => 'fidistr.fsname',
'params' => array(':id' => $_POST['city_id'])
));

relations() in Yii, do not understand the example from the book

I read a book Pactpub Web Application Development with Yii and PHP Nov 2012. Faced with such a problem, I can not understand the logic behind the use of relations (). Here diagram tables in the database:
You need to insert code in the model:
Issue model:
...
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
);
...
Project model:
...
'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
...
I can not understand that we add? If the model Issue understand everything, then the model Project - I do not understand that we are adding. Help to understand ...
If the model Issue understand everything, then the model Project - I
do not understand that we are adding
in some case, you have already had a project, and you would like to find all of issues and partner users of that project.
$project = Project::model()->findByPK(1); // get project id=1
$issues = $project->issues; // get all of issues of project id=1, the result would be array
$users = $project->issues; // get all of users of project id=1, the result would be array
$project = Project::model()->with('issues', 'users')->findAll(); // get all of projects which has issue and user
//you have a user name ABC, and you want to find all of projects which contains a issue from owner has that user name.
$projects = Project::model()->with(array(
'issues' => array(
'alias' => 'issue',
//'condition' => '',
//'params' => array(),
'with' => array(
'owner'=>array(
'alias' => 'user',
'condition' => 'username =:username',
'params' => array(':username'=>'ABC'),
)
)
),
))->findAll();
There has many ways let you mix them up with multiple relations and conditions. One of above example would generate some big SQL SELECT query that I never want to deal with on my own :)
AR Relations Details

Yii - Joins on tables are not working

I want to get the data from two tables. I have used the below code to do so. I am new to Yii, if I am doing it the wrong way, please suggest the right way.
Here is the code of controller:
$dataProvider=new CActiveDataProvider('Users', array(
'criteria'=>array(
'with'=>'leave',
'together'=>true,
'condition'=>'leave.user_id=:user_id',
'params'=>array(':user_id'=>$this->loadModel(Yii::app()->user->getId())->user_id),
),
));
$this->render('admin',array(
'dataProvider'=>$dataProvider,
));
Here is the code of view:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'header' => 'Leave Type',
'name'=>'leave_type',
'value'=>'$data->leave->leave_type',
),
'employee_code',
'username',
'password',
array(
'class'=>'CButtonColumn',
),
),
));
My Problem is:
I am joining two tables User and Leave. I want to get the data of users from Leave table as well as the users table. the above code shows me data from user table, when i try to show data from Leave table it throws me following error:
Trying to get property of non-object
UPDATE
Here is my user model relations:
return array(
'leaves' => array(self::HAS_MANY, 'Leaves', 'leave_id'),
'creator' => array(self::BELONGS_TO, 'Users', 'created_by'),
'updator' => array(self::BELONGS_TO, 'Users', 'modified_by'),
'leave' => array(self::HAS_MANY, 'Leaves', 'user_id'),
);
Here is my leave model relations:
return array(
'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
'creator' => array(self::BELONGS_TO, 'Users', 'created_by'),
'updator' => array(self::BELONGS_TO, 'Users', 'modified_by'),
);
You can't display leave relation, because it's a HAS_MANY, I mean, in your $data->leave you have an array of Leave objects.
You should implement implode function, for example:
'value'=> "implode(', ', array_map(function($object) { return $object->leave_type; },
$data->leave))"
BTW, leave relation i think should be a HAS_ONE instead of HAS_MANY, and in this case, you don't need in an implode function in GridView
'value'=>'$data->leave->leave_type', // WRONG
$data->leave = array of objects and you could not access directly leave_type from it. That array was returned from your below relation:
'leave' => array(self::HAS_MANY, 'Leaves', 'user_id'),
I'm not sure the leave type which you want to access and relate it with user. But in this case, the solution is you can add more relation to just access first leave record of user
'singleLeave' => array(self::HAS_ONE, 'Leaves', 'user_id'), // add this line on Model User
And then you can access it properly
'value'=>'$data->singleLeave->leave_type',
Actually, in the first approach, the $data->leave should populate array of Leaves and from there, each Leave record should have to go into child views, and this is the point where you have to go detail

YII CGridView. Trying to show attribute from other table. Relation not working

This is driving me nuts. I read many responses and tutorials and i can't pin point the problem here.
I have 2 Tables: tblEmpleado and tblProfesion Here is an image of the MySQL relations they have (Marked on red)
I want to display on a CGridView the attribute "Descripcion" from the table tblProfesion when displaying the data from tblEmpleado.
I tried using claveProfesion.descripcion without avail (It shows the INT number of the FK just fine without the ".descripcion" part). Also tried using something like:
array(
'header'=>'tableHeaderName',
'value'=>'(isset($data->claveProfesion)) ? $data->claveProfesion->descripcion : null', )
getting "Trying to get property of non-object".
Here is the Empleados.php relation part of the model code:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'tblcelulars' => array(self::HAS_MANY, 'Tblcelular', 'claveEmpleado'),
'tblcuentabancos' => array(self::HAS_MANY, 'Tblcuentabanco', 'claveEmpleado'),
'idCentroTrabajo' => array(self::BELONGS_TO, 'Tblcentrotrabajo', 'idCentroTrabajo'),
'convenio' => array(self::BELONGS_TO, 'Tblconvenio', 'convenio'),
'claveProfesion' => array(self::BELONGS_TO, 'Tblprofesion', 'claveProfesion'),
);
}
And the CGridView part of the Admin.php View code
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'empleados-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'claveEmpleado',
'nombreEmpleado',
'idCentroTrabajo',
array(
'header'=>'tableHeaderName',
'value'=>'($data->claveProfesion!=null) ? $data->claveProfesion->descripcion : null',
),
'aniosExperiencia',
'telefono2',
'email',
...
array(
'class'=>'CButtonColumn',
),
),
)); ?>
And last, i don't know if its related but in the Model class "Profesiones.php" the relation is as follow:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'tblempleados' => array(self::HAS_MANY, 'Tblempleado', 'claveProfesion'),
);
}
Could anyone help me out with this?
Your column and your relation both have the same name claveProfesion. As such Yii is returning the column instead of the relation when you call claveProfesion. To resolve it rename your relation to something else e.g claveProfesionObject
public function relations()
{
return array(
...
'claveProfesionObject' => array(self::BELONGS_TO, 'Tblprofesion', 'claveProfesion'),
);
}

Agile web development using Yii problems

I am new to Php as well as Yii and you can say that i am a new pilot and yet I am learning flying at A380
issue is that everything is working fine for me until CHAPTER 6
I cannot understand concept of Returning back to the owner and requester dropdowns
I am implementing it side by side and in my case Issue.php did not generated relations ,I just then placed following code
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
);
}
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
);
}
if any one can explain working of this i would be great
plus why self:: used?
thanks in advance

Categories