Yii2 : Active Record Column Aliases - activerecord

I'm using Yii2 framework with advanced template.
I get problem with column alias in my controller file, here's my code:
$models = new ActiveDataProvider([
'query' => User::find()->select(['member'=>'fullname'])
]);
The above query equivalent with:
SELECT fullname AS member FROM User;
I send the data to the view using this code:
return $this->render('view', [
'model' => $models,
]);
I want to call the data in my view using GridView widget, here's my code:
echo GridView::widget([
'dataProvider' => $model,
'columns' => [
'member',
],
]);
However, I got an error that tell me the 'member' parameter is not defined.
How can I show the data of my query by calling the column name? (in my case it using alias)
I really appreciate any kind of helps!!

You should simply declare this attribute in your model :
class User extends ActiveRecord
{
public $member;
Read more : https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#selecting-extra-fields

ActiveDataProvider works only with model attributes. member obviously is not presented there.
First of all, maybe it's better to refactor column names to be more clear instead of writing aliases? I don't see any benefit in your example.
If you nevertheless need to use aliases, as alternative for adding additional properties to class, you can work with them with help of ArrayDataProvider and SqlDataProvider.
Examples of usage:
ArrayDataProvider:
use yii\data\ArrayDataProvider;
$dataProvider = new ArrayDataProvider([
'allModels' => User::find()->select(['member' => 'fullname'])->all(),
]);
SqlDataProvider:
use yii\data\SqlDataProvider;
use yii\db\Query;
...
$query = (new Query())
->select(['member' => 'fullname'])
->from('users');
$command = $query->createCommand();
$dataProvider = new SqlDataProvider([
'sql' => $command->sql,
'params' => $command->params,
'totalCount' => $query->count(),
]);
For more details and features of usage please see official docs.
For your case it's better to use ArrayDataProvider, SqlDataProvider is for more complex queries.
In case of one alias and using model methods adding additional attribute as suggested by soju can be better.
But in my opinion it's useless and it's better to refactor column names in case of some ambiguity.

Related

Two fields overwriting each others values on saving in Backpack for Laravel CRUD (spatie/laravel-tags implementation issues)?

I am trying to use spatie/laravel-tags together with Backpack for Laravel. I have 2 types of tags defined. Currently I have extended the Tag model from spatie/laravel-tags as MyCategory and MyTag and added global scopes to separate the two tag types. This works to the extent that it will display the current categories and tags correctly in Backpack, but when I try to save any changes it will only save the entries in the last field, and delete everything in the first field.
Here is my current field configuration for my CRUD:
$this->crud->addField([
'name' => 'categories',
'label' => 'Categories',
'type' => 'select2_multiple',
'tab' => 'Overview',
'attribute' => 'name',
'model' => 'App\MyCategory',
'pivot' => true,
]);
$this->crud->addField([
'name' => 'tags',
'label' => 'Tags',
'type' => 'select2_multiple',
'tab' => 'Overview',
'attribute' => 'name',
'model' => 'App\MyTag',
'pivot' => true,
]);
When I check Laravel Telescope I see that the same thing happens for both fields. First all current tags (regardless of type) for the item I am saving are deleted, and the new tags from the field are added. This is then repeated for the second field, which of course deletes the tags from the first field that should also be kept.
It seems that GlobalScope on my extended Tag models does not stick around for this part. Is there any way to reintroduce the scopes into the queries run by backpack to get these tags to save correctly?
In my CrudController I created custom update and store functions. See the update example below. This seems to work fine. There are still 2 queries being run, with the second one undoing the first one, but for my purposes this is a good enough workaround to be able to have 2 fields with different tag types in the same form in Backpack, using spatie/laravel-tags.
public function update(UpdateRequest $request)
{
$request = request();
// Merge the values from the two tag fields together into the second field
$request->merge(['tags' => array_merge((array)$request->input('categories'), (array)$request->input('tags'))]);
$redirect_location = $this->traitUpdate($request);
return $redirect_location;
}

Laravel 5.6. How to test JSON/JSONb columns

$this->assertDatabaseHas() not working with JSON/JSONb columns.
So how can I tests these types of columns in Laravel?
Currently, I have a store action. How can I perform an assertion, that a specific column with pre-defined values was saved.
Something like
['options->language', 'en']
is NOT an option, cause I have an extensive JSON with meta stuff.
How can I check the JSON in DB at once?
UPD
Now can be done like that.
I have solved it with this one-liner (adjust it to your models/fields)
$this->assertEquals($store->settings, Store::find($store->id)->settings);
Laravel 7+
Not sure how far back this solution works.
I found out the solution. Ignore some of the data label, Everything is accessible, i was just play around with my tests to figure it out.
/**
* #test
*/
public function canUpdate()
{
$authUser = UserFactory::createDefault();
$this->actingAs($authUser);
$generator = GeneratorFactory::createDefault();
$request = [
'json_field_one' => [
'array-data',
['more-data' => 'cool'],
'data' => 'some-data',
'collection' => [
['key' => 'value'],
'data' => 'some-more-data'
],
],
'json_field_two' => [],
];
$response = $this->putJson("/api/generators/{$generator->id}", $request);
$response->assertOk();
$this->assertDatabaseHas('generators', [
'id' => $generator->id,
'generator_set_id' => $generator->generatorSet->id,
// Testing for json requires arrows for accessing the data
// For Collection data, you should use numbers to access the indexes
// Note: Mysql dose not guarantee array order if i recall. Dont quote me on that but i'm pretty sure i read that somewhere. But for testing this works
'json_field_one->0' => 'array-data',
'json_field_one->1->more-data' => 'cool',
// to access properties just arrow over to the property name
'json_field_one->data' => 'some-data',
'json_field_one->collection->data' => 'some-more-data',
// Nested Collection
'json_field_one->collection->0->key' => 'value',
// Janky way to test for empty array
// Not really testing for empty
// only that the 0 index is not set
'json_field_two->0' => null,
]);
}
Note: The below solution is tested on Laravel Version: 9.x and Postgres version: 12.x
and the solution might not work on lower version of laravel
There would be two condition to assert json column into database.
1. Object
Consider Object is in json column in database as shown below:
"properties" => "{"attributes":{"id":1}}"
It can assert as
$this->assertDatabaseHas("table_name",[
"properties->attributes->id"=>1
]);
2. Array
Consider array is in json column as shown below:
"properties" => "[{"id":1},{"id":2}]"
It can assert as
$this->assertDatabaseHas("table_name",[
"properties->0->id"=>1,
"properties->1->id"=>2,
]);
Using json_encode on the value worked for me:
$this->assertDatabaseHas('users', [
'name' => 'Gaurav',
'attributes' => json_encode([
'gender' => 'Male',
'nationality' => 'Indian',
]),
]);

Laravel router with additional parameters

I have controller which called by this route website.lrv/products/{category-url}
public function categoryProducts($uri, Request $request) { /** some code **/ }
I have a few links that are responsible for ordering products, i added them directly to blade template, like this:
route('client.category.products', [
'url' => Route::current()->url,
'order' => 'article',
'by' => 'desc' ])
route('client.category.products', [
'url' => Route::current()->url,
'order' => 'article',
'by' => 'asc' ])
And the request link with ordering is:
website.com/products/chargers?order=name&by=asc
Now i want to add products filters. I have many filters, each filter can contain many values. The problem is that i don't know how to make route to get some like that:
website.com/products/chargers?width=10,20,30&height=90,120,150
or something like that.
I need to get request array like that:
$request = [
....
filters => [
width => ['10','20','30'],
height => ['90','120','150'],
],
....
];`
If you need some additional info, i will edit my question.
You can access the array you are looking for by using $request->query() within the categoryProducts function.
Edit: Also worth noting you can access them from within your blade template using the Laravel helper function request(), so {{ request()->query('width') }} would return xx where website.lrv/products/category?width=xx.
Edit 2: Note that if your query string includes commas per your example width=10,20,30 the query() will just return a comma separated string, so you would need to explode(',',$request->query('width')) in order to get the array of widths
I think you have to add them manually like this:
route('client.category.products')."?order=name&by=asc";
Try this I hope it help.

Relations in resources?

So I'm planning to start using resources for my "API" (vue endpoint). So I started to search for some tutorials about the subject, and found a youtuber that describes the process. And I started making my own API resource. The youtuber shows briefly how to use the relations, but the thing is that I receive Property [description] does not exist on this collection instance. when trying to use the relation in the resource.
The current setup is:
$stack = Stack::select(['id', 'name', 'subject_id', 'description', 'image'])->where('id', '=', $requestId)->first();
$questions = $stack->load('question.choiceInRandomOrder');
return $questions;
And with resource it would be something like (notice choiceInRandomOrde, I would need that relation also):
return [
'subject' => $this->subject->name,
'name' => $this->name,
'slug' => $this->slug,
'description' => $this->description,
'image' => $this->image,
'questions' => [
'description' => $this->question->description,
'is_info' => $this->question->is_info,
'source' => $this->question->source,
'image' => $this->question->image,
]
];
}
And for testing, I have setup the following in my routes web.php
use App\Stack;
use App\Http\Resources\StackResource;
Route::get('/json', function(){
$stack = Stack::find(2);
return new StackResource($stack);
});
You try to access the name of subject in 'subject' => $this->subject->name, but you do not load the relation.
i don't know if i'm right but i thnk it has to be with the fact that ure not doing an Eloquent call but a Query Builder call (when doing $stack = Stack::select ... ). Why select just some fields in the call if you can choose the parameters to show directly in the model class? (see this).
Try to doing an Eloquent call instead (something like Stack::find(1)) and test it. It should work.

CakePHP data change during validation and beforeSave is not being save with the changes

I'm saving data sent from a form.
In the Controller I am doing :
$this->User->create();
$this->User->save($this->request->data)
The $this->request->data looks like this:
'User' => array(
'password' => '*****',
'username' => 'ddddd',
'role' => '256/aa01bdf80d42beb48dd3225acddf447fdd7a39f3',
'parent_id' => '0/b6ba9bd57f6c70cf738891d4c6fac22abed4161d'
)
There are validation rules that works on 'role' and 'parent_id' to insure the role/parent ids are among those the user can access.
The validation changes the field values if the data is valid.
I also have a Tree behavior that is setting some tree fields in a beforeSave() filter in the behavior.
The validation rule is writing the change to $this->data->[$model][$field] as shown below.
public function checkListHash($check, $field) {
$explodedCheck = explode('/', $check[$field]);
if ($this->secureId($explodedCheck[0], $explodedCheck[1])) {
$this->data['User'][$field] = $explodedCheck[0];
return true;
}
return false;
}
The beforeFilter() in the behavior is changing the data array with statements like this:
$Model->data[$Model->alias][$ancestors] = $ancestorList;
When validation and the beforeFilter() processing is complete, I have a beautiful and correct array of data at $this->User->data that looks like this:
'User' => array(
'password' => '*****',
'active' => '0',
'role' => '256',
'parent_id' => '0',
'node' => '0',
'username' => 'ddddd',
'modified' => '2013-09-15 09:55:02',
'created' => '2013-09-15 09:55:02',
'ancestor_list' => ',0,'
)
However, $this->request->data is unchanged. And that is what is being save.
Clearly I'm not understanding the relationship of these various ways to get to the data. I've tried a variety of ways to address the data in the three contexts:
Controller
Model
Behavior
And I've tried $this->User->create($this->request->data); before the Controller save() statement.
In the controller, what I'm seeing as available data arrays:
PRIOR TO THE SAVE
$this->request->data = $this->data = proper data from the form
$this->User->data = some default, unpopulated array
PRIOR TO THE SAVE when I use $this->User->create($this->request->data)
all three arrays contain raw form data
AFTER THE SAVE in either case
$this->request->data = $this->data = exactly as before
$this->User->data = the properly massaged data
Can anyone sort me out?
Don Drake
Just to explain the data arrays to you, when you submit the form, the data from it is stored in $this->request->data on the controller. You are then modifying $this->User->data from inside the model, which is a different array on the model itself. It would not affect $this->request->data because it's a completely different array which belongs to the controller, and the model has no knowledge of it.
You are then saving the User model using the request data, which remains unchanged from when the form was submitted. This is logical and normal behaviour because you're not actually using the $this->User->data array that you've modified.
Your save might always be failing because the data the model is trying to save isn't the updated data, it's just the basic data from $this->request->data.
Try this:
$this->User->set($this->request->data);
$this->User->save();
Also, if you are using a beforeSave in your model, make sure the method returns true, or it will never actually go on to save.

Resources