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

AggregateModel: Cannot add hasOne reference #984

Closed
mkrecek234 opened this issue Apr 21, 2022 · 2 comments · Fixed by #986 or #987
Closed

AggregateModel: Cannot add hasOne reference #984

mkrecek234 opened this issue Apr 21, 2022 · 2 comments · Fixed by #986 or #987

Comments

@mkrecek234
Copy link
Contributor

The recent implementation of AggregateModel does not allow to add a hasOne reference (or inherit it from the source model).
Steps to replicate:

 $aggregateModel =  new \Atk4\Data\Model\AggregateModel($sourcemodel);
        
        $aggregateModel->hasOne('tax_rule_id', ['model' => [TaxRule::class]]);
        
        $aggregateModel->setGroupBy(['tax_rule_id', 'tax_rate'], [
            'total_gross'                 => ['expr' => 'sum([total_gross])'],
            'total_net'                   => ['expr' => 'sum([total_net])'],
            'total_tax'                   => ['expr' => 'sum([total_tax])'],
        ]);

Expected result:
$targetmodel->ref('tax_rule_id') returns hasOne-linked model.

Result:
Error in vendor/atk4/data/src/Reference.php:224saying TypeError: preg_quote(): Argument #1 ($str) must be of type string, null given

@mkrecek234
Copy link
Contributor Author

@mvorisek with the recent changes, then the error changes to !! ATK4 CORE ERROR - EXCEPTION RENDER FAILED: Atk4\Data\Exception: AggregateModel model does not support this action !!

@mvorisek mvorisek reopened this Apr 21, 2022
@mvorisek
Copy link
Member

mvorisek commented Apr 21, 2022

The exception render issue is fixed in atk4/core#347

the exception itself is valid, here is a repro using atk4/ui demo models:

$product = new Product($app->db);

$aggregate = new AggregateModel($product);
$aggregate->hasOne($product->fieldName()->product_category_id, ['model' => [Product::class]]);
$aggregate->setGroupBy([$product->fieldName()->product_category_id], [
    's' => ['expr' => 'count(*)'],
]);

$aggregate->loadAny()->ref($product->fieldName()->product_category_id);

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

Successfully merging a pull request may close this issue.

2 participants