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

Updates for Search-Form Model. #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions generators/crud/default/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$labels = $generator->generateSearchLabels();
$searchAttributes = $generator->getSearchAttributes();
$searchConditions = $generator->generateSearchConditions();

$tableSchema = $generator->getTableSchema();
echo "<?php\n";
?>

Expand All @@ -32,9 +32,16 @@
/**
* <?= $searchModelClass ?> represents the model behind the search form about `<?= $generator->modelClass ?>`.
*/
class <?= $searchModelClass ?> extends <?= isset($modelAlias) ? $modelAlias : $modelClass ?>

class <?= $searchModelClass ?> extends Model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extending original CRUD model class, while creating a search model is quite widely used practice.
It allows usage of the labels and hints from the original model without necessity of redefining them.

Although extending plain Model in this case may seem to be more consistent, i suppose it should be an option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this at all. I use a lot of relations on my model searches.

Example if I have a sales model and I need to include how many items and the total of each sale, I need to do something like this

$parentClass = get_parent_class(); // we can discuss about this later, ignore it for now()
$query = $parentClass::find()->joinWith(['items', 'store.direction', 'customer'])->select([
    $parentClass::tableName() . '.*',
    'itemNumber' => 'count(disticnt item.id)',
    'totalSale' => 'sum(item.cost)',
]);

where I have a methods getItemNumber(), setItemNumber(), getTotalSale() and setTotalSale() defined on the Sales active record. With this now i have to define that all over. Also other type of validations and validations such as behaviors that I used for a model. Example: set full name property of a user on the after find based on the attributes last name and first name from the db table.

{
<?php foreach ($searchAttributes as $attribute) : ?>/**
* @var <?= ($tableSchema !== false) ? $tableSchema->columns[$attribute]->phpType : 'mixed'?>

*/
public $<?= $attribute ?>;
<?php endforeach ?>


/**
* @inheritdoc
*/
Expand All @@ -45,15 +52,6 @@ public function rules()
];
}

/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}

/**
* Creates data provider instance with search query applied
*
Expand Down