Skip to content
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

Using "through" with find('all') #598

Open
danielcatao opened this issue Sep 18, 2018 · 0 comments
Open

Using "through" with find('all') #598

danielcatao opened this issue Sep 18, 2018 · 0 comments

Comments

@danielcatao
Copy link

danielcatao commented Sep 18, 2018

Greetings.

According to the EER Diagram and the Classes below, how can I produce the Query A?
Can we push the Solution B to better use the joins array?

EER Diagram
eer

Classes

Class Customer extends ActiveRecord\Model
{
	static $has_many = array(array(‘order’), array(‘product’, ‘through’ => ‘order’));
}
Class Order extends ActiveRecord\Model
{
	static $belongs_to = array(array(‘customer’, 'foreign_key' => 'customerid'), array(‘‘product’, 'foreign_key' => 'productid'));
}
Class Product extends ActiveRecord\Model
{
	static $has_many = array(array(‘order’), array(‘customer’, ‘through’ => ‘order’));
}

Call

Customer::all(array('select' => 'customer.name, GROUP_CONCAT(product.name)', 
		    'joins' => array('order','product')));

Query A
SELECT customer.name, product.name, product.value, order.date
FROM customer
JOIN order ON customer.customerid = order.customerid
JOIN product ON product.productid = order.productid

Solution B

  • Add to lib/Relationship.php (line 348)
if(isset($this->options['through'])) 
{ 
    $through_table = Table::load($this->options['through']); 
    $from_table_name = $through_table->get_fully_qualified_table_name(); 
}

Regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant