I currently have an belongsToMany relationship between two Table, Skus and Medias. I named the join table skus_images though.
I'm here trying to save only ids, not inserting new data in an HABTM way.
I have in my form :
echo $this->Form->input('images._ids', ['options' => $images, 'multiple' => 'checkbox']);
And everything is working fine there, I'm correctly getting my Medias listed.
But whenever I try to submit the form, I get this :
Error: Call to a member function get() on a non-object
File /home/weshguillaume/AndyToGaby/vendor/cakephp/cakephp/src/ORM/Association/BelongsToMany.php
Line: 874
I've defined my relationship as such in SkusTable :
$this->belongsToMany('Images', [
'className' => 'Media.Medias',
'joinTable' => 'skus_images',
'targetForeignKey' => 'image_id'
]);
The context doesn't give any insights, neither does the stack trace as it's both (almost) empty. Thanks :)
EDIT:
Controller add method:
public function add($product_id)
{
$skus = $this->Skus->newEntity();
if ($this->request->is('post')) {
$skus = $this->Skus->patchEntity($skus, $this->request->data(), [
'associated' => [
'Attributes'
]
]);
if ($this->Skus->save($skus)) {
$this->Flash->success('The skus has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The skus could not be saved. Please, try again.');
}
}
$attributes = $this->Skus->Attributes->find('list');
$images = $this->Skus->Products->getMedias('list', $product_id, 'photo');
$this->set(compact('skus', 'products', 'attributes', 'images', 'product_id'));
$this->set('_serialize', ['skus']);
}
Controller posted data:
[
'product_id' => '65',
'attributes' => [
'_ids' => ''
],
'reference' => '',
'quantity' => '420',
'is_default' => '0',
'images' => [
'_ids' => [
(int) 0 => '90'
]
]
]
Forgot to add the name of the association in the patchEntity associated option. Still shouldn't throw a fatal error so I created a github ticket.
Related
I encountered above error while update scenario, am trying to save the multi selected values into relation table('classification_vs_metric') along with master tables(classifications,metrics) id's in create metrics.
but when i click on edit button on already created record i encounter this error.
Metrics Master
id
name
type
3
Land
ha,m2
4
Floors
Nos
Classification Master
id
industry
sector
subsector
1
Construction
Commercial
Casino
2
Construction
Commercial
Cinema
3
Construction
Commercial
Convention Center
classification_vs_metric slave/relation table
id
metric_id
classification_id
1
3
1
2
3
2
3
3
3
4
4
1
5
4
2
and am using following method to get the slave table values in actionUpdate in metrics controller
public function getClassificationVsMetrics1()
{
return $this->hasMany(Classificationvsmetric::className(), ['metric_id' => 'id'])->select(['classification_id']);
}
as
public function actionUpdate($id)
{
$model = $this->findModel($id);
$classificationIndustry = array();
$releations = $model->getClassificationVsMetrics1()->asArray()->all();
if ($model->load(Yii::$app->request->post())) {
if($model->save()){
$classifications = Yii::$app->request->post()["Classificationvsmetric"]['classification_id'];
$classVsmetric = Classificationvsmetric::deleteAll(['metric_id'=>$model->id]);
foreach ($classifications as $key => $value) {
$Classificationvsmetric = new Classificationvsmetric();
$Classificationvsmetric->classification_id =(int)$value;
$Classificationvsmetric->metric_id = $model->id;
$Classificationvsmetric->save(false);
}
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
'Classificationvsmetric' => $releations
]);
}
and in _form.php
$classificationIndustry = ArrayHelper::map(\common\models\Classifications::find()->all(),'id',function($model,$default){
return $model["industry"]." - ".$model["sector"] ." - ".$model['sub_sector'];
});
echo $form->field($Classificationvsmetric[0], 'classification_id')->widget(Select2::classname(), [
'data' => $classificationIndustry, // error showing in this line
'value'=>(!$model->isNewRecord ? [$result] : ''),
'language' => 'en',
'options' => ['placeholder' => 'Select classification(s)','multiple' => true],
'pluginOptions' => [
'allowClear' => true,
],
]);
this is working fine with create scenario, but getting error "Call to a member function isAttributeRequired() on array" in edit scenario. Can any body help me !!
Finally after 2 days of head scratching i find solution myself.
I have made few minor changes like below
In metric master
public function getClassificationVsMetrics1()
{
return $this->hasMany(Classificationvsmetric::className(), ['metric_id' => 'id']);
}
In metric controller
public function actionUpdate($id)
{
$model = $this->findModel($id);
$releations = $model->getClassificationVsMetrics1()->all();
if ($model->load(Yii::$app->request->post())) {
if($model->save()){
$classifications = Yii::$app->request->post()["Classificationvsmetric"]['classification_id'];
$classVsmetric = Classificationvsmetric::deleteAll(['metric_id'=>$model->id]);
foreach ($classifications as $key => $value) {
$Classificationvsmetric = new Classificationvsmetric();
$Classificationvsmetric->classification_id =(int)$value;
$Classificationvsmetric->metric_id = $model->id;
$Classificationvsmetric->save(false);
}
if($Classificationvsmetric == true){
return $this->redirect(['view', 'id' => $model->id]);
}
}
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
'Classificationvsmetric' => $releations
]);
}
and in view am managed to pass the values stored in the slave table as default value for select2 widget.
<?php
$classificationIndustry = ArrayHelper::map(\common\models\Classifications::find()->all(),'id',function($model,$default){
return $model["industry"]." - ".$model["sector"] ." - ".$model['sub_sector'];
});
foreach ($Classificationvsmetric as $key => $value) {
$selected[] = $value['classification_id'];
}
echo $form->field($Classificationvsmetric[0], 'classification_id')->widget(Select2::classname(), [
'data' => $classificationIndustry,
'language' => 'en',
'options' => [ 'value'=>(!$model->isNewRecord ? $selected : ''),'placeholder' => 'Select classification(s)','multiple' => true],
'pluginOptions' => [
// 'disabled' => $is_readonly,
'allowClear' => true,
// 'maximumSelectionLength' => 3,
],
]);
?>
i know this is not feasible solution, but right now this will save my butt.
preselected values in select2 widget in update scenario with many to many relation
I'm looking to use elastic search on a project with model relation.
For now elastic search is working, I've followed this doc who explain how to start with this package :
elasticsearch/elasticsearch
babenkoivan/elastic-migrations
babenkoivan/elastic-adapter
babenkoivan/elastic-scout-driver
The problem is I need to able to search by relation.
this is my composant elastic migration :
Index::create('composant', function(Mapping $mapping, Settings $settings){
$mapping->text('reference');
$mapping->keyword('designation');
$mapping->join('categorie');
$settings->analysis([
'analyzer' => [
'reference' => [
'type' => 'custom',
'tokenizer' => 'whitespace'
],
'designation' => [
'type' => 'custom',
'tokenizer' => 'whitespace'
]
]
]);
});
Here my categorie elastic migration :
Index::create('categorie', function(Mapping $mapping, Settings $settings){
$mapping->keyword('nom');
$settings->analysis([
'analyzer' => [
'nom' => [
'type' => 'custom',
'tokenizer' => 'whitespace'
]
]
]);
});
My composant Model :
public function categorie()
{
return $this->belongsTo('App\Model\Categorie');
}
public function toSearchableArray()
{
return [
'reference' => $this->reference,
'designation' => $this->designation,
'categorie' => $this->categorie(),
];
}
and my categorie Model :
public function toSearchableArray()
{
return [
'nom' => $this->nom,
];
}
So if you look at the composant relation, you can see that the join mapping return the categorie relation. I dont now if I do it right but what I know is that elasticsearch didn't have any relation in the object I'm looking for.
And I didn't find any doc of how to use the join mapping method of the package.
OK, I've found the solution, the problem was in the migration you must use object in order to index the belongsToMany relationship like that
Index::create('stages', function (Mapping $mapping, Settings $settings) {
$mapping->text('intitule_stage');
$mapping->text('objectifs');
$mapping->text('contenu');
$mapping->object('mots_cles');
});
and in your model :
public function toSearchableArray()
{
return [
'intitule_stage' => $this->intitule_stage,
'objectifs' => $this->objectifs,
'contenu' => $this->contenu,
'n_stage' => $this->n_stage,
'mots_cles' => $this->motsCles()->get(),
];
}
And the result is as expected now
If you want to get "nom" of categorie, write this in composant Model instead
'categorie' => $this->categorie->nom ?? null,
$this->categorie() return the relationship, not the object.
Same problem with a belontoMany relation, and I've made the same things in order to get the relation as a nested object, but when I try to populate my index the field "mots_cles" stay empty, I don't understand why.
Here is the migration :
Index::create('stages', function (Mapping $mapping, Settings $settings) {
$mapping->text('intitule_stage');
$mapping->text('objectifs');
$mapping->text('contenu');
$mapping->nested('motsCles', [
'properties' => [
'mot_cle' => [
'type' => 'keyword',
],
],
]);
});
The model :
public function toSearchableArray()
{
return [
'intitule_stage' => $this->intitule_stage,
'objectifs' => $this->objectifs,
'contenu' => $this->contenu,
'n_stage' => $this->n_stage,
'mots_cles' => $this->motsCles(),
];
}
public function motsCles()
{
return $this->belongsToMany(MotsCle::class);
}
I want to make the list of table who have relation only, example:
Table:
Order
Process
In ProcessCrudController I want to replace the Process list view into Order list view that only have relation (order-process).
The solution I've tried:
Create new function in OrderCrudController return view($this->crud->getListView(), $this->data);
then, addClause using $request->type URL
The problem of that solution:
Not showing the action row in table
Easy to show all query if we try to remove URL
I really want to make the list view of Process that only already have relation to Order, or any suggestion/idea to make this happen?
NB: I'm struggling at this problem I didn't found the solution, help me please
EDIT (Adding Code):
OrderCrudController:
protected function setupListOperation()
{
// This is the solution that I described before
// $request = request();
// $this->crud->addClause('where', 'user_id', '=', $request->type ?? 1);
$this->crud->addColumns([
[
'name' => 'personal_name',
'label' => 'Name',
'type' => 'text',
],
[
'name' => 'notes',
'label' => 'Catatan',
'type' => 'textarea',
],
[
'name' => 'user_id',
'label' => 'Dibuat oleh',
'type' => 'select',
'entity' => 'user',
'attribute' => 'name',
'model' => 'App\User',
],
]);
}
ProcessCrudController:
protected function setupListOperation()
{
CRUD::setFromDb();
// This table should be listing Order's query and that only have a process (already created custom_create_view)
// That's why i want to take Order's table and not making new custom_list_view
}
you don't have to make a new view, you just modify the query result using addClause ....
in your ProcessCrudController in setupListOperation() add your clause:
$this->crud->addClause('has','order');
assuming that the relation name in Process model that point to Process is "order"
In the initialize function of OrdersTable.php, I have
$this->hasOne('CurrentReview', [
'className' => 'Reviews',
'finder' => 'latest',
'foreignKey' => 'order_id
]);
and in ReviewsTable.php, I have
public function findLatest(query $q)
{
return $q->orderDesc('id')->limit(1);
}
I am trying to get only the latest review associated with the order, but I am only ever able to get the first one.
$order = $this->Orders->get($id, ['contain' => [
'CurrentReview'
]]);
What am I missing?
I am almost getting what I need with
$order = $this->Orders->get($id, ['contain' => [
'Reviews' => function ($q) {
return $q->find('latest');
}
]]);
but it's inside an array that I don't want
i am need to sort some fields (asc,desc) in GridView, but same fields are calculated. Look at code below:
SearchModel:
class ObjectSearch extends Object {
use SearchModelTrait;
public function rules()
{
return [
['id', 'integer', 'min' => 1],
];
}
public function search($params)
{
$this->company_id = \Yii::$app->user->identity->companyId;
$query = Object::find()->where(['company_id' => $this->company_id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);
$dataProvider->setSort([
'attributes' => [
'id',
'name',
'lastReportResult' => [
'asc' => ['lastReportResult' =>SORT_ASC ],
'desc' => ['lastReportResult' => SORT_DESC],
'default' => SORT_ASC
],
'reportPercentDiff'
]
]);
if (!($this->load($params,'ObjectSearch') && $this->validate())) {
return $dataProvider;
}
$this->addCondition($query, 'id');
return $dataProvider;
}
Methods in Object model:
public function getLastReportResult()
{
$lastReport = $this->getLastReport();
$message = 0;
if (!empty($lastReport)) {
$statistic = new ReportStatistic($lastReport);
$message = $statistic->getPercent();
}
return $message;
}
/**
* #return int
*/
public function getReportPercentDiff()
{
$lastReport = $this->getLastReport();
$message = 0;
if (!empty($lastReport)) {
$statistic = $lastReport->getReportDiff();
if (!empty($statistic['diff'])) {
$message = $statistic['diff']['right_answers_percent_diff'];
} elseif (!empty($statistic['message'])) {
$message = $statistic['message'];
}
}
return $message;
}
So, by this methods, i am calculating a values of two fields, which are need's sorting. This way doesn't working, i have a Database Exception, because object table hasn't this fields. exception
How to do sorting of this fields ?
Update: I am the author of this answer and this answer is not accurate. Preferred way is to use database view
Add two public properties to ObjectSearch.php and mark it as safe
class ObjectSearch extends Object {
use SearchModelTrait;
public $lastReportResult, $reportPercentDiff;
public function rules()
{
return [
['id', 'integer', 'min' => 1],
[['lastReportResult', 'reportPercentDiff'], 'safe']
];
}
public function search($params)
{
$this->company_id = \Yii::$app->user->identity->companyId;
$query = Object::find()->where(['company_id' => $this->company_id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);
$dataProvider->setSort([
'attributes' => [
'id',
'name',
'lastReportResult' => [
'asc' => ['lastReportResult' =>SORT_ASC ],
'desc' => ['lastReportResult' => SORT_DESC],
'default' => SORT_ASC
],
'reportPercentDiff' => [
'asc' => ['reportPercentDiff' =>SORT_ASC ],
'desc' => ['reportPercentDiff' => SORT_DESC],
'default' => SORT_ASC
],
]
]);
if (!($this->load($params,'ObjectSearch') && $this->validate())) {
return $dataProvider;
}
$this->addCondition($query, 'id');
return $dataProvider;
}
Then in index.php (view file in which you are having grid view) add lastReportResult and reportPercentDiff in array of all attributes (list of all attributes ob Object model)
...
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// your other attribute here
'lastReportResult',
'reportPercentDiff',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
...
For more info you can visit Kartik's blog at Yii
Though this is an old thread, stumbled upon this and tried to find other method to achieve sorting of purely calculated field to no avail... and this post unfortunately is not an answer as well... It just that I feel the need to post it here to give a heads up to those that still looking for the solution so as not to scratch their heads when trying the solution given and still fail.
The given example from documentation or referred links as far as I have tested only works if you have a column within the database schema (whether in the main table or the related tables). It will not work if the virtual attribute/calculated field you create is based on calculating (as an example multiplication of 2 column on the table)
e.g:
table purchase: | purchase_id | product_id | quantity |
table product: | product_id | unit_price |
then, if we use a virtual attribute 'purchase_total' for model 'purchase' which is the multiplication of quantity and unit_price (from the join table of purchase and product on product_id), eventually you will hit an error saying 'purchase_total' column can not be found when you tried to sort them using the method discussed so far.