Want to display name instead of ID - yii

I am new to Yii framework. So i need help.
I have 2 tables
table A
admin_id admin_name
1 aaa
2 sss
3 eee
table B
id admin_id phone_num
1 1 123123123
2 1 234234234
3 2 343434344
So when displaying table B i want to display admin_name from table A instead of admin_id
I have a relation defined in Table B model as
return array('admin' => array(self::BELONGS_TO, 'table A', 'admin_id'),);
I want to use relations concept to get the admin name.
One more important thing is I am not using views of Yii. So i want only controller and model to be used.
Kindly help me out with this issue.

You can get the value with:
$model = TableB::model()->findBy....();
echo $model->admin->admin_name;
See http://www.yiiframework.com/doc/guide/1.1/en/database.arr for more details on lazy and eager loading.
Btw: It's recommended not to prefix your column names.

I displayed company category name inside company admin page using CGridView as under:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'company-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
array(
'name'=>'company_category_id',
'value'=>'$data->companyCategory->category_name',
),
'uan',
'website',
array(
'class'=>'CButtonColumn',
),
),
));

Related

Prefix or append a string to meta_title, meta_key, meta_description in category page

Category page meta_title, meta_key and meta_description tags come from the table ps_category_lang.
mysql> select * from ps_category_lang limit 1;
+-------------+---------+---------+-------+-------------+--------------+------------+---------------+------------------+
| id_category | id_shop | id_lang | name | description | link_rewrite | meta_title | meta_keywords | meta_description |
+-------------+---------+---------+-------+-------------+--------------+------------+---------------+------------------+
| 1 | 1 | 1 | Raíz | | raiz | | | |
+-------------+---------+---------+-------+-------------+--------------+------------+---------------+------------------+
1 row in set (0.00 sec)
Is it possible to add a prefix (or suffix) to those three values, so it uses the information from the database but it appends or prefixs a certain value?
If so, what shall be needed to be done? I already have a custom module overriding the category page with extended template and controller.
Prestashop 1.7.1
The best way is by overriding the /classes/controller/FrontController.php, specifically the method getTemplateVarPage() in the code:
$page = array(
'title' => '',
'canonical' => $this->getCanonicalURL(),
'meta' => array(
'title' => $meta_tags['meta_title'],
'description' => $meta_tags['meta_description'],
'keywords' => $meta_tags['meta_keywords'],
'robots' => 'index',
),
'page_name' => $page_name,
'body_classes' => $body_classes,
'admin_notifications' => array(),
);
Here you could validate the current page and alterate it as your needs.
for each standard controller in PrestaShop, you have a dedicated function in the Meta class, in your case, the getCategoryMetas() function that you can override and adapt to fit your needs.
You also can use the previous answer to rewrite the metas firstly computed in the Meta::getCategoryMetas() in CategoryController::getTemplateVarPage() function.
Good luck

Getting join table data into Yii2 gridview

I have 3 tables that i basically need to display in the single gridview.
property
sale (FK property_id)
sale_staff (FK sale_id, staff_id)
staff
Now a staff member can be a lister, a seller, or both, and each sale can have multiple lister or sellers.
So table sale_staff has column 'staff_type' with possible enum values lister',seller`.
So the gridview must have a display something like
property street | sale price | sale date | lister | seller
and the lister/seller fields should be able to display simple initials comma separated like MJ, AB. This initial field comes from the staff table, which as above is linked to sale_staff via id.
I have not seen this type of scenario before so im simply unsure of how to incorporate the sale_type into the search so that only a staff listed as lister or seller is shown in that place.
Also, if i try something like this, just to see if anything is pulled back:
'attribute' => 'saleStaff.staff_id.initials'
or
'attribute' => 'saleStaff.staff'
I get (not set) in the gridview.
I have relations set up as:
Sale:
public function getProperty()
{
return $this->hasOne(Property::className(), ['id' => 'property_id']);
}
public function getSaleStaff()
{
return $this->hasMany(SaleStaff::className(), ['sale_id' => 'id']);
}
Sale_Staff:
public function getStaff()
{
return $this->hasOne(Staff::className(), ['id' => 'staff_id']);
}
public function getSale()
{
return $this->hasOne(Sale::className(), ['id' => 'sale_id']);
}
How do I match up the join table sale_staff with the sale table ? Is the structure ok or is DB incorrect ? I have done it this way to be flexible - any number of staff can be either a lister, seller, or both such as:
property street | sale price | sale date | lister | seller
Parkers Road | 400,000 | 22/06/2016| MJ | MJ, AB
Naturally, key question is how to represent multiple (unknown number of) values in a single gridview column like that.

Write a join query with multiple tables in ruby on rails

I have Downloaded Document model which contains data regarding the user downloaded documents , the schema of downloaded_documents table is
id document_id document_type user_id
1 444 article 1
2 120 judgment 2
3 444 article 1
4 555 rule 3
5 555 rule 4
Now I want to generate report for all the downloaded documents and it users. And there is no relationship between the downloaded documents and user model, and each document_type in nothing but the each model in our application, so I want a document information also user information also. So How I write a query ?
I have written the following query:
DownloadedDocument.joins("JOIN users ON downloaded_documents.user_id = users.id").select("distinct on (downloaded_documents.document_id, downloaded_documents.user_id, downloaded_documents.document_type) users.email, document_type.created_at").where('created_at >=? and created_at <= ?',Date.parse(from_date), Date.parse(to_date))
But I am not getting the document information. I should add a one more JOIN in the above query which is nothing but JOIN on ------downloaded_documents.document_id = -----.id in the ---- place I need to add document type name, but for ALL I want all models mentioned in the document_type, so how could I write query?
My Article model schema
ID title
444 first title
Judgment schema
ID appellant_name respondent_name case_no
120 ddd gggg abc.123
Rule schema
ID rule_name
555 example
I want a output something like this:
{'judgment' => [{case_no: 'abc.123',email: 'abc.example.com', dcoument_id: 120}], 'article' => [{title: 'first title', email: 'ads#example.com', document_id: 444},{title: 'first_title', email: 'fff#example.com', document_id: 444}]}
I want to generate a report for to list document and its users

Lithium Model find query with <> operator

I'm trying to fetch rows excluding the one which already are in an other table :
| Content | ContentSelected
| –––––––– | ––––––––––––––––––––––––-
| id | content_id | operation_id
| 1 | 1 | 9999
| 2 | 3 | 1000
| 3
=> 2,3
Here how I tried to run this query :
Content::find('all', array(
'joins' => array(new Query(array(
'source' => 'ContentSelected',
'constraints' => array('ContentSelected.operation_id' => 9999),
'conditions' => array(
'Content.id' => array('<>' => 'ContentSelected.content_id')
));
Here the SQL query run by the adapter :
SELECT * FROM "Content" AS "Content"
JOIN "ContentSelected" ON "ContentSelected"."operation_id" = 1
WHERE ("Content"."id" <> 0);
Is there another way to performed a query excluding results, or to force the adapter to write ContentSelected.content_id instead of 0 in the where clause ?
Thanks
You can use Model::connection()->read() to run a native sql in Li3. Check this out http://www.jblotus.com/2011/10/25/executing-raw-mysql-queries-in-lithium/
Content::connection()->read($your_sql);
But before using such a solution. Try to implement your model properly using Model Relationships (http://li3.me/docs/manual/models/relationships.md) so that you don't need to use a native query and let the model does the work for you.

Grouped aggregations with Yii STAT?

I have a Yii STAT Relation that's defined to provide a grouped SUM result, however when I access the relation in my View, the only value is the latest single value rather than each value.
For example, here's my relation:
'total_salaries_by_job' => array(
self::STAT,
'Employee',
'department_id',
'select' => 'job_type_id, SUM(salary)',
'group'=>"job_type_id"
)
This generates the following SQL:
SELECT
department_id AS c
, job_type_id
, SUM(salary) AS s
FROM Employee AS t
WHERE t.department_id = 1
GROUP BY
department_id
, job_type_id
Running that manually, the result set is:
c | job_type_id | s
------+----------------+---------
1 | 1 | 233000
------+----------------+---------
1 | 2 | 25000
------+----------------+---------
1 | 3 | 179000
However, in my view, if I do the following:
<pre>
<?php print_r($department->total_salaries_by_job); ?>
</pre>
The result is simply: 179000, whereas I was expecting it to be an array with 3 elements.
Is returning just 1 value the way STAT relations work or is there something else I need to be doing?
Is it possible to do what I'm attempting?
You can do what you are after, but you can't use a STAT relationship to do it. Rather, use a Normal HAS_MANY relationship and use your same select statement.