How to add ng-model to Symfony form type checkboxes - angularjs

I have a few checkboxes in a symfony form like
->add('countries', EntityType::class, [
'class' => 'Catalog:Countries',
'choice_label' => 'name',
'multiple' => true,
'expanded' => true
])
how can I add ng-model to each checkbox to bind them to my angularJS script?

You're looking for the attr option for the parent <select> element or choice_attr option for the child <option> elements.
Example:
$builder->add('countries', EntityType::class, [
'class' => 'Catalog:Countries',
'choice_label' => 'name',
'multiple' => true,
'expanded' => true,
'attr' => [
'[ng-model]' => 'countries'
],
'choice_attr' => [
'[selected]' => 'isCountrySelected()'
]
]);

I found the solution here
choice_attr
however, if some of the checkboxes are selected then you will loose this information on page load

Related

Custom widget in Elementor with the same controls

I am creating a custom Elementor widget with two image controls, unfortunately, I can only get one of the two to work in the content tab. I thought if I add two sections it would work but seems not, looked at Elementor documentation at https://developers.elementor.com/docs/editor-controls/control-media/ but cant find anything there.
$this->start_controls_section(
'section_image_one',
[
'label' => esc_html__( 'Image One' , $this->domain ),
]
);
$this->add_control(
'image',
[
'label' => esc_html__( 'Choose Image', $this->domain ),
'type' => Controls_Manager::MEDIA,
'dynamic' => [
'active' => true,
],
'default' => [
'url' => Utils::get_placeholder_image_src(),
],
]
);
$this->end_controls_section();
$this->start_controls_section(
'section_image_two',
[
'label' => esc_html__( 'Image Two' , $this->domain ),
]
);
$this->add_control(
'image',
[
'label' => esc_html__( 'Choose Image', $this->domain ),
'type' => Controls_Manager::MEDIA,
'dynamic' => [
'active' => true,
],
'default' => [
'url' => Utils::get_placeholder_image_src(),
],
]
);
$this->end_controls_section();
It looks like it is a rather simple solution
.....
$this->add_control(
'image',
.....
As mentioned on https://developers.elementor.com/docs/editor-controls/regular-control/, the "image" mentioned is a control name and needs to be unique.

Cakephp 4 : How to save hasone data

I have two database table 1) users 2) profiles
profiles has a field called bank_ac
I am trying to save it from user model.
I have created form input like
<?= $this->Form->create($user) ?>
<?= $this->Form->control('profile.bank_ac'); ?>
<?= $this->Form->end() ?>
User model I have added associative like
$this->hasOne('Profiles');
After debug getting data like
[
'name' => 'Jone',
'email' => 'abcd#yahoo.com',
'profile' => [
'bank_ac' => '1212212'
]
]
after debug patch entity
object(App\Model\Entity\User) {
'name' => 'Jone',
'email' => 'abcd#yahoo.com',
'[new]' => true,
'[accessible]' => [
'name' => true,
'email' => true,
'created' => true,
'modified' => true
],
'[dirty]' => [
'name' => true,
'email' => true,
'profile' => true
],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Users'
}
In UsersController/add I have applied code like
public function add(){
$user = $this->Users->newEmptyEntity();
$user = $this->Users->patchEntity($user, $this->request->getData());
$this->Users->save($user, ['associated' => ['Profiles']]);
}
Profile data not saving , also not getting any error. How can I save this associative data ?
Looking at your entity debug result, the profile field is missing from the accessibility config, hence disallowing its use in mass assignment (patching).
Add it to your User::$_accessible property, and it should work:
protected $_accessible = [
// ...
'profile' => true,
];
See also
Cookbook > Database Access & ORM > Entities > Mass Assignment

Controller doesn't get hasOne relationship data - Cakephp

I have a problem with related tables in CakePHP. I can't get the related table data include in the form.
I have two Entities. One of them is "Users" and the other one is "Subjects". Every User has a subject. Table "Subject" has foreign key idUser from Users table.
I added in UsersTable:
$this->hasOne('Subjects');
And I added in SubjectsTable:
$this->belongsTo('Users', [
'foreignKey' => 'idUser',
'joinType' => 'INNER'
]);
In the view (signup), I have this:
<div class="form-group">
<?php echo $this->Form->control('Subject.name',['label' => 'Asignatura','placeholder' => 'Ingrese asignatura','class' => 'form-control']) ?>
</div>
In the controller, I have this:
$user = $this->Users->patchEntity($user, $this->request->getData(),['associated' => 'Subjects']);
When I debug $user, I am getting this result:
\src\Controller\UsersController.php (line 113)
object(App\Model\Entity\User) {
'id' => '11111111',
'name' => 'Leo',
'firstlastname' => 'Messi',
'secondlastname' => 'Cuccittini',
'email' => 'leo.messi#gmail.com',
'password' => '$2y$10$E02nd/w89BDvgCyz36bQdeBbujOLrSdON1e6CD25aDYCP2VeLkNNm',
'role' => '2',
'[new]' => true,
'[accessible]' => [
'id' => true,
'name' => true,
'firstlastname' => true,
'secondlastname' => true,
'email' => true,
'password' => true,
'role' => true
],
'[dirty]' => [
'id' => true,
'name' => true,
'firstlastname' => true,
'secondlastname' => true,
'email' => true,
'password' => true,
'role' => true
],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Users'
}
So, I am not getting in the controller the data from Subject.
Any help, please.
Model
$this->hasOne('Subjects', [
'foreignKey' => 'userId'
]);
Controller:
$user = $this->User->get($id, ['contain' => ['Subjects']);
Entity/User.php
protected $_accessible = [
'subjects' => true
// ...
];
Form
https://book.cakephp.org/3.0/en/views/helpers/form.html#associated-form-inputs
Change: Subject.name to user.subject.name
<?php echo $this->Form->control('user.subject.name',['label' => 'Asignatura','placeholder' => 'Ingrese asignatura','class' => 'form-control']) ?>

cakephp 3.x Saving Nested (deep) Association

I have product data coming from a 3rd party service call that I then create an object from and save to my MySQL DB. My models are as follows:
'products' hasMany>> 'product_skus' hasMany>> 'product_sku_attributes'
table relationships
In my ProductsTable.php initialize() method I have:
$this->hasMany('ProductSkus', [
'foreignKey' => 'product_no',
'dependent' => true,
]);
In my ProductSkusTable.php initialize() method I have:
$this->hasMany('ProductSkuAttributes', [
'foreignKey' => 'product_sku_id',
'bindingKey' => 'id',
'propertyName' => 'product_sku_attributes',
'dependent' => true,
]);
My controller:
$products = TableRegistry::get('Products');
$entity = $products->newEntity($product_data[0]);
$products->save($entity, [
'associated' => [
'ProductSkus',
'ProductSkus.ProductSkuAttributes',
]
]);
Here's is the relevant snippet from my entity debug:
'product_skus' => [
(int) 0 => object(App\Model\Entity\ProductSkus) {
'sku' => 'BDS1401H',
'sku_price' => (float) 366.76,
'sku_weight' => (float) 38.1,
'sku_img_main' => '',
'sku_img_large' => '',
'sku_img_default' => false,
'is_default' => true,
'product_sku_attributes' => [
(int) 0 => [
'product_no' => (int) 23200,
'sku' => 'BDS1401H',
'attribute_name' => 'Front Sway Bar Links',
'option_name' => 'Stock'
],
(int) 1 => [
'product_no' => (int) 23200,
'sku' => 'BDS1401H',
'attribute_name' => 'Shock Options',
'option_name' => 'NX2 Series'
],
(int) 2 => [
'product_no' => (int) 23200,
'sku' => 'BDS1401H',
'attribute_name' => 'Steering Stabilizer Options',
'option_name' => 'Stock'
]
],
'[new]' => true,
'[accessible]' => [
'*' => true,
'id' => true
],
'[dirty]' => [
'sku' => true,
'sku_price' => true,
'sku_weight' => true,
'sku_img_main' => true,
'sku_img_large' => true,
'sku_img_default' => true,
'is_default' => true,
'product_sku_attributes' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'ProductSkus'
},
(int) 1 => object(App\Model\Entity\ProductSkus) { ...
I doubled checked, and all my fields are set as accessible in my table entity classes. Also, at this point I'm only trying to save one product record for simplicity, hence $products->newEntity().
My data is saving to 'products' and 'product_skus' tables without problem, but not to 'product_sku_products'. Can anyone see what the problem is? Is it because I'm not using the same foreignKey?
Please let me know what else I can provide for clarity.
The product_sku_attributes data is not being marshalled, it's still an array of arrays, and not an array of entities, hence it's not being saved.
Just like when saving entities, creating/patching them with associated data by default only works for first level associations. Deeper nested associations require to specify them via the associated option, ie:
$entity = $products->newEntity($product_data[0], [
'associated' => [
'ProductSkus.ProductSkuAttributes'
]
]);
$products->save($entity, [
'associated' => [
'ProductSkus.ProductSkuAttributes'
]
]);
See also
Cookbook > Database Access & ORM > Saving Data > Converting Request Data into Entities
Cookbook > Database Access & ORM > Saving Data > Saving Associations

How can I show attribute value with check Box in Yii 2 GridView

i like to put mission_id in checkbox
$req = 'select c.mission_id as mission_id, c.user_id as user_id from order c';
$dataProvider = new SqlDataProvider([
'sql' => $req,
]);
return $this->render('factures', [
'dataProvider' => $dataProvider,
]);
in _index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'mission_id',
'user_id',
],
]); ?>
This it's ok, but when i use checkbox, it s KO : Trying to get property of non-object
[
'attribute' => 'id',
'format' => 'raw',
'value' => function($data) {
return '<input type="checkbox" name="chk_group" value="'.$data->mission_id.'" />Mission : '.$data->mission_id;
},
],
Your help please
a some response : Trying to get property of non-object with :
[
'attribute' => 'id',
'format' => 'raw',
'value' => function($model) {
return '<input type="checkbox" name="chk_group" value="'.$model->mission_id.'" />Mission : '.$model->mission_id;
},
],

Resources