How to get related values in Yii? - php

Schema:
CITY
ID (int)
Name (string)
Status (int)
CITY_STATUS
ID (int)
Name (string)
When I display a city (in the View view), I want to display the related CITY_STATUS.Name value, instead of the CITY.Status value
When I add or update a city, I want to display a drop down of all CITY_STATUS.Names in the drop down
How do I do this in Yii?

Guess I'll answer it myself.
Question 1
Relations are easier if you setup a foreign key in your database first. To do this you need to use MySQL (not SQLite) with the InnoDB engine (not MyISAM), and the field in question needs an index on it. Then, Gii will setup the relations function for you automatically. Otherwise, you'll have to do it manually in the relations() function of the model in question.
To use a related value in a View:
In protected/views/[model name]/view.php, in the CDetailView attributes array, change
'Status'
to
array('label'=>'Status', 'value'=>$model->RelationName->Name)
where RelationName is the name of the relation
To use a related value in an Index view, change protected/views/[model name]/_view.php (note the underscore), for example in this case you would change
$data->Status
to
$data->RelationName->Name
To use a related value in an Admin view, in the CGridView widget call, in the columns array, replace say
'Status'
with
array('name'=>'Status', 'header'=>'Status', 'value'=>'$data->RelationName->Name')
(note the use of the variable $data, and not say $model or $dataProvider). Still trying to figure out how to sort and filter...
Question 2
To use a drop-down menu, in protected/views/[model name]/_form.php:
change
<?php echo $form->textField($model,'Status'); ?>
to
<?php echo $form->dropDownList($model,'Status', CHtml::listData(Status::model()->findAll(), 'ID', 'Name')); ?>
el chief, you are a gentleman and a scholar.

Related

Changing own id in pivot table

I'm developing a Laravel application where I have a posts table, a tags table and a post_tag table which acts as a pivot table.
Now I need to give all the tags from a post to another post. In other words I need to make:
$tags = $post->tags;
And change the post_id to each record in the pivot table. I have all the relationships already set.
EDIT: this is my code
class Post extends Model
{
public function tags()
{
return $this->belongsToMany(Tag::class)->withPivot('is_active')->withTimestamps();
}
}
class Tag extends Model { }
The main problem is that I have to keep the is_active value as it is. I just need to replace the post_id from the pivot table where post_id equals the one I want to override (I know I could make a raw query but I'm trying to avoid it)
EDIT 2:
I made it work this way but I still prefer an object oriented way
DB::table('post_tag')->where('post_id', $post_a->id)->update(['post_id' => $post_b->id]);
You can use the method updateExistingPivot of Eloquent, from the laravel documentation:
If you need to update an existing row in your pivot table, you may use updateExistingPivot method. This method accepts the pivot record foreign key and an array of attributes to update:
$user = App\User::find(1);
$user->roles()->updateExistingPivot($roleId, $attributes);
Try something like
$tags = $post->tags;
//convert tags to IDs only for upcoming steps
... //an array of IDs
$post->tags()->sync($tags); //remove the tags from this post
$post2->tags()->sync($tags); // add the tags to this post
This should get you to the right tracks.
Update
If it was only one, this would do
$post2->tags()->attach($tag, ['is_active' => true]);
$post2->tags()->->sync([1 => ['is_active' => true], 2 => ['is_active' => true]);
You can try to adapt the example above.
But I have no idea how to do it with an array of IDs.

Yii active record relation limit to one record

I am using PHP Yii framework's Active Records to model a relation between two tables. The join involves a column and a literal, and could match 2+ rows but must be limited to only ever return 1 row.
I'm using Yii version 1.1.13, and MySQL 5.1.something.
My problem isn't the SQL, but how to configure the Yii model classes to work in all cases. I can get the classes to work sometimes (simple eager loading) but not always (never for lazy loading).
First I will describe the database. Then the goal. Then I will include examples of code I've tried and why it failed.
Sorry for the length, this is complex and examples are necessary.
The database:
TABLE sites
columns:
id INT
name VARCHAR
type VARCHAR
rows:
id name type
-- ------- -----
1 Site A foo
2 Site B bar
3 Site C bar
TABLE field_options
columns:
id INT
field VARCHAR
option_value VARCHAR
option_label VARCHAR
rows:
id field option_value option_label
-- ----------- ------------- -------------
1 sites.type foo Foo Style Site
2 sites.type bar Bar-Like Site
3 sites.type bar Bar Site
So sites has an informal a reference to field_options where:
field_options.field = 'sites.type' and
field_options.option_value = sites.type
The goal:
The goal is for sites to look up the relevant field_options.option_label to go with its type value. If there happens to be more than one matching row, pick only one (any one, doesn't matter which).
Using SQL this is easy, I can do it 2 ways:
I can join using a subquery:
SELECT
sites.id,
f1.option_label AS type_label
FROM sites
LEFT JOIN field_options AS f1 ON f1.id = (
SELECT id FROM field_options
WHERE
field_options.field = 'sites.type'
AND field_options.option_value = sites.type
LIMIT 1
)
Or I can use a subquery as a column reference in the select clause:
SELECT
sites.id,
(
SELECT id FROM field_options
WHERE
field_options.field = 'sites.type'
AND field_options.option_value = sites.type
LIMIT 1
) AS type_label
FROM sites
Either way works great. So how do I model this in Yii??
What I've tried so far:
1. Use "on" array key in relation
I can get a simple eager lookup to work with this code:
class Sites extends CActiveRecord
{
...
public function relations()
{
return array(
'type_option' => array(
self::BELONGS_TO,
'FieldOptions', // that's the class for field_options
'', // no normal foreign key
'on' => "type_option.id = (SELECT id FROM field_options WHERE field = 'sites.type' AND option_value = t.type LIMIT 1)",
),
);
}
}
This works when I load a set of Sites objects and force it to eager load type_label, e.g. Sites::model()->with('type_label')->findByPk(1).
It does not work if type_label is lazy-loaded.
$site = Sites::model()->findByPk(1);
$label = $site->type_option->option_label; // ERROR: column t.type doesn't exist
2. Force eager loading always
Building on #1 above, I tried forcing Yii to always to eager loading, never lazy loading:
class Sites extends CActiveRecord
{
public function relations()
{
....
}
public function defaultScope()
{
return array(
'with' => array( 'type_option' ),
);
}
}
Now everything always works when I load Sites, but it's no good because there are other models (not pictured here) that have relations that point to Sites, and those result in errors:
$site = Sites::model()->findByPk(1);
$label = $site->type_option->option_label; // works now
$other = OtherModel::model()->with('site_relation')->findByPk(1); // ERROR: column t.type doesn't exist, because 't' refers to OtherModel now
3. Make the reference to the base table somehow relative
If there was a way that I could refer to the base table, other than "t", that was guaranteed to point to the correct alias, that would work, e.g.
'on' => "type_option.id = (SELECT id FROM field_options WHERE field = 'sites.type' AND option_value = %%BASE_TABLE%%.type LIMIT 1)",
where %%BASE_TABLE%% always refers to the correct alias for table sites. But I know of no such token.
4. Add a true virtual database column
This way would be the best, if I could convince Yii that the table has an extra column, which should be loaded just like every other column, except the SQL is a subquery -- that would be awesome. But again, I don't see any way to mess with the column list, it's all done automatically.
So, after all that... does anyone have any ideas?
EDIT Mar 21/15: I just spent a long time investigating the possibility of subclassing parts of Yii to get the job done. No luck.
I tried creating a new type of relation based on BELONGS_TO (class CBelongsToRelation), to see if I could somehow add in context sensitivity so it could react differently depending on whether it was being lazy-loaded or not. But Yii isn't built that way. There is no place where I can hook in code during query buiding from inside a relation object. And there is also no way I can tell even what the base class is, relation objects have no link back to the parent model.
All of the code that assembles these queries for active records and their relations is locked up in a separate set of classes (CActiveFinder, CJoinQuery, etc.) that cannot be extended or replaced without replacing the entire AR system pretty much. So that's out.
I then tried to see if I can create "fake" database column entries that would actually be a subquery. Answer: no. I figured out how I could add additional columns to Yii's automatically generated schema data. But,
a) there's no way to define a column in such a way that it can be a derived value, Yii assumes it's a column name in way too many places for that; and
b) there also doesn't appear to be any way to avoid having it try to insert/update to those columns on save.
So it really is looking like Yii (1.x) just does not have any way to make this happen.
Limited solution provided by #eggyal in comments: #eggyal has a suggestion that will meet my needs. He suggests creating a MySQL view table to add extra columns for each label, using a subquery to look up the value. To allow editing, the view would have to be tied to a separate Yii class, so the downside is everywhere in my code I need to be aware of whether I'm loading a record for reading only (must use the view's class) or read/write (must use the base table's class, does not have the extra columns). That said, it is a workable solution for my particular case, maybe even the only solution -- although not an answer to this question as written, so I'm not going to put it in as an answer.
OK, after a lot of attempts, I have found a solution. Thanks to #eggyal for making me think about database views.
As a quick recap, my goal was:
link one Yii model (CActiveRecord) to another using a relation()
the table join is complex and could match more than one row
the relation must never join more than one row (i.e. LIMIT 1)
I got it to work by:
creating a view from the field_options base table, using SQL GROUP BY to eliminate duplicate rows
creating a separate Yii model (CActiveRecord class) for the view
using the new model/view for the relation(), not the original table
Even then there were some wrinkles (maybe a Yii bug?) I had to work around.
Here are all the details:
The SQL view:
CREATE VIEW field_options_distinct AS
SELECT
field,
option_value,
option_label
FROM
field_options
GROUP BY
field,
option_value
;
This view contains only the columns I care about, and only ever one row per field/option_value pair.
The Yii model class:
class FieldOptionsDistinct extends CActiveRecord
{
public function tableName()
{
return 'field_options_distinct'; // the view
}
/*
I found I needed the following to override Yii's default table data.
The view doesn't have a primary key, and that confused Yii's AR finding system
and resulted in a PHP "invalid foreach()" error.
So the code below works around it by diving into the Yii table metadata object
and manually setting the primary key column list.
*/
private $bMetaDataSet = FALSE;
public function getMetaData()
{
$oMetaData = parent::getMetaData();
if (!$this->bMetaDataSet) {
$oMetaData->tableSchema->primaryKey = array( 'field', 'option_value' );
$this->bMetaDataSet = TRUE;
}
return $oMetaData;
}
}
The Yii relation():
class Sites extends CActiveRecord
{
// ...
public function relations()
{
return (
'type_option' => array(
self::BELONGS_TO,
'FieldOptionsDistinct',
array(
'type' => 'option_value',
),
'on' => "type_option.field = 'sites.type'",
),
);
}
}
And all that does the trick. Easy, right?!?

Kohana add function return province name

I'm back again for another question, i'm trying and trying. But i can't get it fixed.
This is my issue;
I have a database table, with a ProvinceID this can alter from 1 to 12. But it's an ID of the province. The provinces are stored with the value pName in the table Provinces
I have the following code to alter the table, and join it with the preferences table
$veiling = ORM::factory('veilingen')
->select('veilingvoorkeur.*')
->join('veilingvoorkeur', 'LEFT')
->on('veilingen.id', '=', 'veilingvoorkeur.vId')
->find_all();
$this->template->content = View::factory('veiling/veilingen')
->bind('veiling', $veiling);
It displays correctly, in the view i have;
echo '<div class="row">';
foreach($veiling as $ve)
{
echo $ve->provincie;
}
?>
</div>
it displays the provincie id; but i want to add a function to it; So it will be transformed to a province name. Normally i would create a functions.php file with a function getProvince($provinceId)
Do a mysql query to grab the pName value from Provinces and that is the job. But i'm new to kohana. Is there an option to turn the province id to province['pName'] during the ORM selection part, or do i have to search for another solution. Which i can't find :(
So. Please help me on the road again.
Thanx in advance.
Kind regards,
Kevin
Edit: 1:08
I've tried something, and it worked. I used ORM in the view file, for adding a function;
function provincie($id){
$provincie = ORM::factory('provincies', $id);
return $provincie->pName;
}
But i'm not glad with this way of solution, is there any other way? Or am i have to use it this way?
Check out Kohana's ORM relationships. If you use this, you could access the province name by simply calling:
$ve->provincie->name;
The relationship definition could look something like:
protected $_belongs_to = array(
'provincie' => array(
'model' => 'Provincie',
'foreign_key' => 'provincie_id'
),
);
Note that if you have a table column called provincie the relationship definition I give above will not work. You'd either have to change the table column to provincie_id or rename the relationship to e.g. provincie_model.
The output you describe when you do echo $ve->provincie; suggests that you store the ID in a column called provincie, thus the above applies to you.
I'd personally go for the first option as I prefer accessing IDs directly with an _id suffix and models without any suffix. But that's up to you.
Edit: If you use Kohanas ORM relationships you could even load the province with the initial query using with() e.g:
ORM::factory('veiling')->with('provincie')->find_all();
This could save you hundreds of extra queries.

CakePHP form select value as table.id, select option text as table.textfield

I'm new to Cake. I've gotten some things set up, relevantly a "users" and a "profiles" table, controller, model and view. In my profiles/add view, I'm attempting to output a select input with the value being the user.id and the option text being the user.username. Ultimately, the value user.id will be saved in profile.user_id.
I have read through a lot of documentation, and successfully set up the select to output the options from the user tables, but it uses the user.id as both the value and the option text.
Controller:
$this->set('users', $this->Profile->User->find('list'));
View:
echo $this->Form->input('user_id');
It seems like this sort of thing would be routine, so feel free to answer with just a link to the documentation if I've overlooked it completely.
Thanks!
successfully set up the select to output the options from the user tables, but it uses the user.id as both the value and the option text.
find('list') will by default use the primary key as the key, and the model's displayField as the value.
If a model does not have an explicit displayField but has a field named title or name Cake will automatically pick this field, otherwise it will not guess and simply use the primary-key field - which is what's happening in the question.
You can however simply specify which fields you want in the results of your find('list') call. Therefore either:
Set the model displayField
class User extends AppModel {
public $displayField = 'username';
}
and then use:
$users = $this->User->find('list');
Explicitly choose the fields
$users = $this->User->find('list', array(
'fields' => array('id', 'username')
));
In both cases - the result will be e.g.:
array(
1 => 'firstuser',
...
76 => 'AD7six'
)

Yii activeCheckBoxList

I have a strange probem
In AR model, Gift i have many_many relation with categories
'categories'=>array(self::MANY_MANY, 'GiftCategory',
'tbl_category_gift(gift_id, category_id)'),
And when i call $gift->categories it returns active record objects, and in database i see that values is stored correctly
But when i write
<? echo $form->checkBoxList($model, "categories", GiftCategory::listData()); ?>
In my checkboxlist only 1 value is selected if model have categories, i dont understand wtf is it, please, help :)
PROBLEM: Yii activeCheckBoxList displaying only 1 checked (only first)
It doesn't work because, in the words of the Yii creator Qiang:
...in activeListBox(), the second parameter should be an attribute
name, not a relation name. In your case, you may need to declare a new
property to store the selections.
I believe the same thing applies to activeCheckBoxList.
The way I solved this was as follows (this blog post was helpful Retrieving selected checkbox items in yii)
Add index option to your relation
'categories' => array(self::MANY_MANY,
'Category', 'post_category(post_id, category_id)','index'=>'id'),
Add a property to your model
public $selectedCategoryIds
Populate this attribute in afterFind
public function afterFind()
{
parent::afterFind();
$this->selectedCategoryIds = array_keys($this->categories);
}
Use this new "attribute in your view
<? echo $form->checkBoxList($model, "selectedCategoryIds", GiftCategory::listData()); ?>
In order to properly handle updating this data see Larry Ullman's article Handling Related Models in Yii Forms
<?php
echo CHtml::activeCheckBoxList($model, 'categories',
CHtml::listData(GiftCategory::model()->findAll(), 'id', 'title'),
array('checkAll' => ' Select All'));
?>

Categories