Symfony backend: Load data on combo box depending on another - php

Im starting with symfony and im a little lost here. Of course nothing that a good tutorial can manage. However i've come to a point that my tutorial doesn't cover.
Creating the backend i write this generator.yml
...product/config/generator.yml
generator:
class: sfDoctrineGenerator
param:
model_class: Product
theme: admin
non_verbose_templates: true
with_show: false
singular: ~
plural: ~
route_prefix: product
with_doctrine_route: true
actions_base_class: sfActions
config:
actions:
fields:
product_type_id: {label: Product Type}
product_name: {label: Product Name}
list:
display: [product_type_id, category, product_name, created_at, updated_at]
filter: ~
form: ~
edit: ~
new:
display: [product_type_id, product_name, url, description, _category]
The last field is rendered throught a custom file _category.php
<?php
echo '<HTML CODE><select><option>...</option>';
$query = Doctrine_Core::getTable('category')
->createQuery('c')
->where("id <> 1");
$quer= $query->fetchArray();
foreach($quer as $q) {
echo '<option value="'.$q['id'].'">'.$q['category_name'].'</option>';
}
echo '</select> </HTML CODE>'
?>
It give a simple form to introduce the data, and its ok. But i was trying that the first combo box on the "new" action (product_type_id) allow me to modify the query of categories to display only those that match.
I wonder if i can access the value of product_type_id invoking some sfFuction or doctrine... or something.
Thanks in advance for your help.

Related

Property structure reorder in Octobercms

I am using Octobercms/Wintercms and I identified in the documentation (https://docs.octobercms.com/2.x/backend/reorder.html#configuring-a-behavior) that there is the possibility of reordering the records of a relationship, adding only one structure option in the config_relation.yaml file, however I have already added this configuration in N ways and it does not work, it does not appear in the list of the data the drag and drop option.
Attempts I made:
# ===================================
# Relation Behavior Config
# ===================================
tickets:
label: 'cashless.events::lang.relations.tickets'
view:
list: $/cashless/events/models/ticket/columns.yaml
toolbarButtons: create|delete
manage:
form: $/cashless/events/models/ticket/fields.yaml
recordsPerPage: 10
structure:
showTree: true
showReorder: true
maxDepth: 2
dragRow: true
Attempts 2:
tickets:
label: 'cashless.events::lang.relations.tickets'
view:
list: $/cashless/events/models/ticket/columns.yaml
toolbarButtons: create|delete
structure:
showTree: true
showReorder: true
maxDepth: 2
dragRow: true
manage:
form: $/cashless/events/models/ticket/fields.yaml
recordsPerPage: 10
Attempts 3
tickets:
label: 'cashless.events::lang.relations.tickets'
view:
list: $/cashless/events/models/ticket/columns.yaml
toolbarButtons: create|delete
manage:
form: $/cashless/events/models/ticket/fields.yaml
recordsPerPage: 10
structure:
showTree: true
showReorder: true
maxDepth: 2
dragRow: true
Nothing works.

Create new list field type for EasyAdmin

With EasyAdmin Symfony bundle, with Symfony 4.2, how create a new list field type?
Use case
"I want to display a link to show page in the list table"
(not a form type, a list type):
easy_admin:
entities:
offer:
class: App\Entity\Offer
list:
fields:
- { property: name, type: MY_TYPE??? }
You have 2 solutions i believe :
If the url is stored in your object there is a custom type for this :
https://symfony.com/doc/2.x/bundles/EasyAdminBundle/book/list-search-show-configuration.html#url-data-type
It allows you to display an url :
# config/packages/easy_admin.yaml
easy_admin:
entities:
Product:
class: App\Entity\User
list:
fields:
- { property: 'blogUrl', type: 'url' }
If you don't have the full url you can try by using a custom template :
https://symfony.com/doc/2.x/bundles/EasyAdminBundle/tutorials/custom-property-options.html#custom-property-options
This way you can define a custom template to generate your url and pass a param if you need :
# config/packages/easy_admin.yaml
easy_admin:
entities:
Product:
class: App\Entity\Product
list:
fields:
# ...
- { property: 'tags', template: 'admin/tag_collection.html.twig',
label_colors: ['primary', 'success', 'info'] }

Seed multiple tables from one backend/builder form?

I'm trying to insert data into multiple related tables from one (builder) back end form/model - but it seems I'm doing something wrong.
t1 posts (used for model Posts.php)
id, flag1, flag2 , created_at, updated_at
t2 post_content (used for model Posts_content.php)
id, content
I've tried expanding the model (Posts.php) used for the form as explained in the relations documentation of octobercms like so:
public $hasOne = ['content' => ['Test\Gcn\Models\Post_content', 'key' => 'id', 'otherKey' => 'id']];
While this does not produce an error when a record is created via the backend controller, no data is actually written to posts_content.
I've also tried to solve it with a proxy field
fields.yaml (of model Posts.php)
post_content[content]:
label: 'Post here'
size: ''
mode: tab
span: full
type: markdown
Posts.php (with the proxy field)
public function formExtendModel($model)
{
/*
* Init proxy field model if we are creating the model
*/
if ($this->action == 'create') {
$model->post_content = new Post_content;
}
return $model;
}
According to the error message, the array needs to be set to be jsonable. But even after that it looks like it's trying to insert the data in the wrong table.
What is the proper way to achieve this?
I'm trying to have one form where a user can enter some flags (checkboxes and the like) and a text field which should be inserted into post_content table with the correct id.
I appreciate your time and help, thank you!
From my opinion, you have to do many changes in your structure like below:
1) Add content_id in your posts table
id, flag1, flag2, content_id, created_at, updated_at
And second table should be contents table
id, content
content_id will be used to create one to one relationship between Post and Content. For more information about relations click here.
2) Then, you have to define models Post for posts and Content for all contents.
In Content model give One to one relationship.
public $hasOne = [
'post' => 'Test\Gcn\Models\Post'
];
If you give this type of relation, this will find content_id in your Post model so it set direct relationship between Post and Content.
3) Your form field should be like
In Post model fields.yaml should be
fields:
flag1:
label: 'Flag 1'
oc.commentPosition: ''
span: left
type: text
flag2:
label: 'Flag 2'
oc.commentPosition: ''
span: auto
type: text
And In Content model fields.yaml should be
fields:
post:
label: Post
oc.commentPosition: ''
nameFrom: flag1
descriptionFrom: description
span: auto
type: relation
content:
label: Content
size: small
oc.commentPosition: ''
span: left
type: textarea
and columns.yaml should be
columns:
content:
label: Content
type: text
searchable: true
sortable: true
post:
label: Post
select: flag1
relation: post
searchable: true
sortable: true
For more details about the backend, you can go here...1) form relation 2) column relation.
This all is from my side. Now analyze all this and try your code. And tell me if you have any query.
Thank you.

OCTOBERCMS Dropdown options dependent on selected value on other dropdown

I'm stuck with this problem and i can't figure out how to solve after some time searching for a example.
The two dropdowns options are table dependent on their values.
I have the one table with 'area' values (nested with simple tree working ok) with the following structure on fields.yaml file:
fields:
id:
label: Número
oc.commentPosition: ''
span: auto
disabled: 1
type: number
area_id:
label: 'Parente de'
oc.commentPosition: ''
emptyOption: 'Sem valor'
span: auto
type: dropdown
area:
label: Área
oc.commentPosition: ''
span: full
required: 1
type: text
I also have another table 'modulos' values with the following structure in fields.yaml:
fields:
modulo:
label: Módulo
oc.commentPosition: ''
span: auto
required: 1
type: text
area:
label: Área
oc.commentPosition: ''
nameFrom: area
emptyOption: 'Sem valor'
span: auto
descriptionFrom: id
type: relation
In the 'Area' model I have:
...
public $hasMany = [
'modulos' => ['JML\Gkb\Models\Modulos']
];
In the 'Modulos' model I have
....
public $belongsTo = [
'area' => ['\JML\Gkb\Models\Area']
];
I have other model that have relations with previous fields and two dropdown fields working ok without any filter, and the troubleshoting field (modulos) where I can't find a way to filter based on the values of 'Area' dropdown I have the following in fields.yaml.
....
modulo_id:
label: mod
oc.commentPosition: ''
emptyOption: 'Sem valor'
span: auto
required: 1
dependsOn:
area
type: dropdown
tab: Geral
In my models PHP file where I have the dropdowns defined, I have:
public function getModuloIdOptions() {
return Modulos::where('area_id', '=', $this->area)->lists('modulo', 'id');
}
That to me seems logical (maybe not) and I tried with DB also and many more other. I tried with dd() to see if I can get the values from the first dropdown to no avail. If I try to filter the values, no value appears at all (except an empty value).
Any help out there ???
TIA
JL
The dataset is passed as the second argument to get the "getOptions" method. Here is an alternative approach that may work:
public function getModuloIdOptions($value, $data) {
return Modulos::where('area_id', '=', array_get($data, 'area'))->lists('modulo', 'id');
}
You may also want to try accessing the area_id value:
public function getModuloIdOptions(){
return Modulos::where('area_id', '=', $this->area_id)->lists('modulo', 'id');
}
Or less efficiently the area->id value (may require exception handling):
public function getModuloIdOptions(){
return Modulos::where('area_id', '=', $this->area->id)->lists('modulo', 'id');
}
I solved the problem with that dropdown and others with the same objective with the following steps:
I have the the 'Relation' widget on them. Changed them to 'Dropdown' widget.
Defined the 'Depends on' field.
Defined the 'Preset' field to the one above. I think that is the missing link to the problem solution not documented anywhere and i get there on try/error basis (may be it is valuable to add this to the October documentation).
Filter options with the snipet code on the end of my question or the second snipet of Samuel answer.
That solved my problem.
Thanks all.

Symfony i18n field and admin generator

I use symfony 1.4.15 with doctrine. I have module and there are two i18n field. So in my form class I make:
$this->languages = sfConfig::get('app_cultures_enabled');
$langs = array_keys($this->languages);
$this->embedI18n($langs);
foreach($this->languages as $lang => $label)
{
$this->widgetSchema[$lang]['name'] = new sfWidgetFormTextarea(array(), array('cols'=>40,'rows'=>2));
$this->widgetSchema[$lang]['description'] = new sfWidgetFormTextarea(array(), array('cols'=>80,'rows'=>5));
}
And it is work perfect!
But I have many fields in my form, so I need to make some "groups" in my form. So I make next in my generator.yml:
config:
form:
display:
Main info:[active,position,сoverage_id,basis_id,durability_id,comfort_id,weight_coating,thickness_coating,height_of_pile,segment_id,width_rolls_one,width_rolls_two,width_rolls_three,standart_width_rolls,max_width_rolls,min_width_rolls]
Price: [price,margin_on_roll,margin_on_cutting,special_discount,discount_for_residues]
Market: [certificate]
And I can not display two i18n field "name" and "description". I try
Market: [name_i18n] and name_i18n_uk and many other. So is is to possible to make?If not are there other way to group filed in form?
Thank you!
What you can do is something like this in the generator:
form:
display:
Market: [certificate, pt, en, es, ...]

Categories