I have a Candidate doctrine entity, this entity has a many to many association to a entity Group
On the index page i show all candidates.
Now i made a form with a ObjectMultiCheckbox object for the Group entity.
After selecting a checkbox and submitting the form i use the doctrine QueryBuilder to only show the candidates that have a association with the selected entity.
This works fine but in the form i'd like to show how many candidates are associated with a certain group.
I can do this using the label_generator option in ObjectMultiSelectCheckbox definition in the fieldset:
namespace MeCandidate\Form;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
class SearchFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct($objectManager)
{
parent::__construct($name = 'search');
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'function',
'attributes' => array(
'class' => 'searchCriteria'
),
'options' => array(
'label' => 'Group',
'object_manager' => $objectManager,
'target_class' => 'MeCandidate\Entity\Group',
'label_generator' => function($targetEntity) {
return ' ' . $targetEntity->getName() . '('. $targetEntity->getCandidates()->count() .')';
},
),
)
);
}
public function getInputFilterSpecification()
{
return array(
array(
'name' => 'group',
'required' => false,
),
);
}
}
This all works as expected.
So when i select Group X it shows all candidates that have a association with Group X.
But now that only a selection of all candidates is shown the number of candidates associated with a certain group is no longer accurate as this shows the total number of candidates associated with the group but i need the number of candidates associated with the group IN the current selection.
I think i can fix it by injecting the current QueryBuilder into the form when creating the form. However i can't find a good way to get the right count.
I hope what i'm trying to do is clear, if not ask me anything.
UPDATE:
I have a working solution, in my Group entity i have a method getCandidateCount it takes a ArrayCollection of the current selection of candidates and checks if a associated candidate is in the selection. I adapted the label_generator function in the fieldset to call getCandidateCount
The problem with this is that after validating the form i have to recreate the form and inject the current selection of candidates into it so i can pass it to the getCandidateCount method inside the label_generator function.
It works but not very elegant so if anybody has ideas about how to it better i would love to hear them!
Thanks,
Yoram
Related
I currently have a users table and a books table, with a pivot table user_book which has user_id, book_id as well as book_tag (this can be 'H' for happy, 'S' for sad or 'A' for angry)
Against the advice of the backpack team, we are looking to have three multiselect options, which will popoulate with the 3 different types of book tags, i.e. Happy books, Sad books, and Angry books.
I currently have the following definition inside the initFields function:
<?php
namespace App\Http\Controllers\Admin;
class UserCrudController extends UserCrudController
{
// ....
protected function initFields()
{
// crud fields here
$this->crud->addField([
'label' => "Happy books",
'type' => 'select2_multiple',
'name' => 'books_h',
'entity' => 'books',
'model' => "App\Models\Book",
'pivot' => true,
]);
}
}
This however, does not seem to save. Any assistance is greatly appreciated
you need to make sure the the relation books_h is defined correctly in your entity as belongsToMany relationship based on laravel docs https://laravel.com/docs/9.x/eloquent-relationships#many-to-many
like so
public function books_h():
\Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Book::class, 'user_book', 'user_id', 'book_id', 'id', 'id')->withPivot('book_tag');
}
then you need to overwrite both create and update methods to update the request for these fields to transfer it to look like so
[
1 => ['book_tag' => 'H'],
2 => ['book_tag' => 'H'],
]
before calling $response = $this->traitUpdate();
refer to this link for more info https://backpackforlaravel.com/docs/5.x/crud-operation-update#override-the-update-method
but I would recommend to use the correct way backpack team mentions https://backpackforlaravel.com/docs/5.x/crud-fields#save-additional-data-to-pivot-table even if you want to have it as 3 separate fields you can define the subfields as hidden with the value you want
I'm currently new to Yii and have some problems in my app.
I have 2 AR (Employee and Children) with the relations is Employee has_many Children. Now when we go into the view of Employee (in the view&id=blablabla not in index), I want to list all the children that the Employee have.
I have made a function in EmployeeController class to retrieve the children from my db
$anak = Anak::Model()->findAll(array(
'condition'=>'id_karyawan=:id_karyawan',
'params'=>array(':id_karyawan'=>$id_karyawan)));
//return $anak;
foreach ($anak as $data){
return $data->namaanak;
}
the problem is, it only showing 1 data (the first one, to be exact) of the children, even though in my db the Employee has 3 children. When I try to count of query result, it's showing 3.
My employee/view is like this
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id_karyawan',
'nama',
array(
'name'=>'idjabatan',
'value'=>$model->findByPk($model->id_karyawan)->jabatan->namajabatan,
),
array(
'name'=>'gaji',
'value'=>Yii::app()->numberFormatter->formatCurrency($model->findByPk($model->id_karyawan)->jabatan->gaji, 'Rp'),
),
'alamat',
array(
'name'=>'ttl',
'value'=>$model->tempatlahir.', '.Yii::app()->dateFormatter->format("dd MMMM yyyy", $model->tgllahir),
),
array(
'name'=>'Istri',
'value'=>Istri::model()->getNama($model->id_karyawan),
'type'=>'raw'
),
array(
'name'=>'Anak',
'value'=>$this->getAnak($model->id_karyawan),
'type'=>'raw'
)
),
I have tried to google it, but i couldn't find any working answer :(
PS: karyawan = employee, anak = children.
You are only getting the first related value because you are returning it right away inside your foreach loop in the function. Gather all the related models first then return that and work with that data.
You can also skip that extra function inside your controller. If you use Gii, it should have already set up the proper Employee HAS_MANY children relation for you, so you could use that to pull all related models for each Employee model you are looking at. See below for a quick example
Employee model:
public function relations()
{
return array(
'children' => array(self::HAS_MANY, 'Child', 'employee_id'),
);
}
View file, passing an employee model to CDetailView:
$this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'attributes'=>array(
array(
'name' => 'Children',
'value' => function ($data) {
// Get all related children using the relation defined in the Employee model and use CHtml::listData to store data inside the $children variable as an array using `id` as key and `child_name` as value
$children = CHtml::listData($data->children, 'id', 'child_name');
// Return names as a comma separated list
return implode(', ', $children);
},
'type'=>'raw'
)
)
);
My tables/column names probably don't match up with what you have but hopefully this helps.
I'm creating a form for a logged-in user to change their password, so I created a subclass of an existing password-reset form I have available. The forms will be identical except with an additional field for existing password. It's worked so far, except I can't figure out a way to manually set the order the new field; the only place I've gotten it to appear is at the end of the form. It seems that ZF2 requires you to add() form elements in the order that you want them rendered. I would do so, except the subclass form's constructor must the parent form's constructor before it can add new fields, by which point the parent form has already added its fields.
I have already tried setting the property order of my new field, but it did not work; I've tried several different combinations (I can't find the documentation for this feature anywhere, after lots of searching).
Subclass constructor snippet:
class ChangePassword extends ResetPassword implements InputFilterProviderInterface {
public function __construct() {
parent::__construct();
$this->add(array(
'type' => 'Zend\Form\Element\Password',
'name' => 'existingPassword',
'order' => 0,
'options' => array(
'label' => 'Existing Password',
'order' => 0,
),
'attributes' => array(
'required' => 'required',
'order' => 0,
)
));
}
Parent constructor snippet:
class ResetPassword extends Form implements InputFilterProviderInterface {
public function __construct() {
parent::__construct('reset-password');
$this->add(array(
'type' => 'Zend\Form\Element\Password',
'name' => 'password',
...
The key you're looking for which affects element order is named priority.
The form add() method accepts a second array containing $flags, and it's in this array that you must add the priority key/value pair.
Your constructor should end up looking something like this ...
class ChangePassword extends ResetPassword implements InputFilterProviderInterface {
public function __construct() {
parent::__construct();
$this->add(array(
'type' => 'Zend\Form\Element\Password',
'name' => 'existingPassword',
'options' => array(
'label' => 'Existing Password',
),
'attributes' => array(
'required' => 'required',
)
), // add flags array containing priority key/value
array(
'priority' => 1000, // Increase value to move to top of form
));
}
}
I have encouter this issue today, Crisp's answer helped but I think it would be nice to precise this :
In the view we have a lot of options to show our form :
<?= $this->form($form)?>
<?= $form->get('{element}') ?>
loop over $form->getIterator()
loop over $form->getElements()
etc...
I have to say i used a lot this structure in all of my projects :
<?php foreach ($form->get('fieldset')->getElements() as $elementName => $element): ?>
<?= $this->partial('partial/formElement', ['element' => $element])?>
<?php endforeach ?>
The problem is : getElements does not use priority, so its just give the element in order of when it was instanciated.
In the view we have to use the iteration method ($form->getIterator()) to get back this flag priority.
I have 2 tables namely Equipment and Supply.
I have used array_merge in yii php to serve as union for two different tables for the purpose of diplaying them in a single grid.
Everything works fine with the fields that is common with the two tables. The problem is when I try to display a field that is only existing in one of these two tables. It says Property "Supply.equipType" is not defined" because only equipment has the equipType relation.
in my gridView:
array(
'name'=>'equipment_type',
'value'=>'$data->equipType->name',
),
in my controller where I did the merging:
$prov1 = new CActiveDataProvider('BaseEiEquipItem', array(
'criteria' => array(
'condition' => 'id>0'
)));
$prov2 = new CActiveDataProvider('BaseSiReceivedItem', array(
'criteria' => array(
'condition' => 'id>0'
)));
$records=array_merge($prov1->data , $prov2->data);
$provAll = new CArrayDataProvider($records,
array(
'sort' => array( //optional and sortring
'keyField'=>false,
'attributes' => array(
'id', 'description',),
),
'pagination' => array('pageSize' => 10) //optional add a pagination
)
);
$this->render('create',array(
'model'=>$model,
'searchModel'=>$searchModel,
'modelGrid'=>$modelGrid,
'provAll' => $provAll,
));
in my equipment model :
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'equipType' => array(self::BELONGS_TO, 'BaseRefEquipmentType', 'equipment_type'),
);
}
Any idea on how to solve this? Is there any way to fake a relation or something?
thanks in advance
Just add condition to check if equipType is there:
'value'=>'isset($data->equipType) ? $data->equipType->name : ""'
Using sql to merge results would be better, though.
Can't you just use plain SQL union?
If Equipment has 2 fields (a, b) and Supply has 2 fields (b, c), you can do union like this:
SELECT a, b, null FROM Equipment
UNION ALL
SELECT null, b, c FROM Supply
This problem shows, that it's probably not a good idea to mix different model types in one gridview. If you have a model that has no equipType then it's pretty obvious, that you can't show this column in a gridview. So what would you expect?
As a (dirty) workaround you can add all missing columns as pseudo attributes to the models where they are missing:
public $equipType;
I'm trying to add the following functionality, however I'm not sure where to start. Any advice, examples, or direction would be greatly appreciated.
I want to add button to the cgridview of the main model in this context. Each of the records available in the cgridview for this model, have a unique attribute called lot for example R3XSEF9
There is another secondary table/model in my database that has records with this same lot attribute. However, this table only has certain records out of all the possible records, sometimes duplicates, and has a set of different attributes.
What I'd like to do is, using the lot attribute, for example lot R3XSEF9 from my cgridview, search the secondary table to see if there is one ore more corresponding rows which contains that same lot R3XSEF9.
If so, I would like the button to be displayed in my CButtonColumn and link to the views for those corresponding models of the secondary table. If not, I would like no button to appear.
Thanks for any help. If any clarification is required, I would be happy to do so.
First of all you need to link tables using "relations" function in model class. If you use FOREIGN KEY constraint in DB relations already filled.
SQL statement:
CREATE TABLE Model1
(
...
FOREIGN KEY(lot) REFERENCES MainModel(lot) ON UPDATE CASCADE ON DELETE RESTRICT,
...
)
Model class:
class MainModel extends CActiveRecord
{
...
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'lots' => array(self::HAS_MANY, 'Model2', 'lot'),
);
}
Then you can use custom button column in your grid (view file) like this:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'main-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
...
array(
'class' => 'CButtonColumn',
'template' => '{lots}',
'header' => 'Lots',
'buttons' => array(
'lots' => array(
'label' => 'Lots',
'imageUrl' => Yii::app()->request->baseUrl.'/img/....png',
'url' => 'Yii::app()->createUrl("controller1/lotlistbymainid", array("id" => $data->id))',
'visible' => 'count($data->lots) > 0',
),
),
),
Explanation of button parameters to be passed thru "buttons" array you can find here. Especialy this part:
buttons property
public array $buttons;
the configuration for additional buttons. Each array element specifies a single button which has the following format:
'buttonID' => array(
'label'=>'...', // text label of the button
'url'=>'...', // a PHP expression for generating the URL of the button
'imageUrl'=>'...', // image URL of the button. If not set or false, a text link is used
'options'=>array(...), // HTML options for the button tag
'click'=>'...', // a JS function to be invoked when the button is clicked
'visible'=>'...', // a PHP expression for determining whether the button is visible
)