-
Notifications
You must be signed in to change notification settings - Fork 446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mapper and Virtual Field #1254
Comments
What does the logged command look like (if it is logged)? |
My impression is that virtual fields should be individual snippets from the
documentation at https://fatfreeframework.com/3.7/sql-mapper#VirtualFields
. SUM(score) and MAX(score) being given there.
You appear to be inserting a whole query where a column would be expected?
(But I'd expect an error from the underlying resultant query, unless it's
being deliberately omitted.)
Alternatively put parentheses round the SELECT and add an alias?:
$this->category_name = '(SELECT categories.name AS category_name FROM
categories LEFT JOIN products p on categories.id = p.category_id) as
category_name';
Another alternative would be to create a view which produces the data
you're after based upon the two tables, and query the view instead.
…On Sat, May 14, 2022 at 6:29 PM Andrew Mark ***@***.***> wrote:
I have a mapper set up like so...
class ProductsModel extends DB\SQL\Mapper
{
public function __construct(DB\SQL $db)
{
parent::__construct($db,'products');
}
public function all(): array
{
$this->category_name = 'SELECT categories.name AS category_name FROM
categories LEFT JOIN products p on categories.id = p.category_id';
return $this->load();
}
}
Based on using virtual fields as suggested in the docs, I am trying to get
a category_name from another table (categories) related as per the left
join above. However, nothing is returning, no category_name is appearing in
data.
return $this->load(); is working fine...I can see the columns from the
products table as configured in the construct.
I am perhaps misunderstanding the purpose of virtual columns. Can anyone
provide me with a hint on this.
Thank you in advance!
—
Reply to this email directly, view it on GitHub
<#1254>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA6B4LQKH3JTBOVQVX3UZFDVJ7PHFANCNFSM5V53GT6A>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
--
PJH
|
I think I am a step closer. For the sake of clarity, I am including the print_r output. ProductsModel Object
(0.3ms) SELECT
(0.3ms) SELECT
) So...no errors now BUT, the field (category_name), which is now returning the correct value (thank you @Rayne and @pauljherring) for the pointer is not coming through under fields but under adhoc. This is (perhaps?) the intended behaviour...but how to retrieve that value so I can include it in a template. For the other fields (in a template, I am just using ((@row.id}}, {{@row.name}} etc and they are appearing in the template but {{@row.category_name}} is not showing / rendering...and no error...like, it's actually not there. I must be nearly there in figuring this? :-) |
BTW...this is my updated model that pushes the above...
|
OK. So actually, this is now working. Seems I need to remove cache...when I did this...the value appeared in the table rows. Thanks to everyone for tips though...all helped! |
That's good news. Don't forget to close the issue. :-) |
I have a mapper set up like so...
class ProductsModel extends DB\SQL\Mapper
{
public function __construct(DB\SQL $db)
{
parent::__construct($db,'products');
}
public function all(): array
{
$this->category_name = 'SELECT categories.name AS category_name FROM categories LEFT JOIN products p on categories.id = p.category_id';
return $this->load();
}
}
Based on using virtual fields as suggested in the docs, I am trying to get a category_name from another table (categories) related as per the left join above. However, nothing is returning, no category_name is appearing in data.
return $this->load(); is working fine...I can see the columns from the products table as configured in the construct.
I am perhaps misunderstanding the purpose of virtual columns. Can anyone provide me with a hint on this.
Thank you in advance!
The text was updated successfully, but these errors were encountered: