Zend_Select select static value - zend-framework

How to make a request with the Zend_Select
SELECT "subdivision" as `type`, a.id as id FROM `some_table` a;
doing so
$ this-> select ()
-> from (
array ('a' => 'some_table'), array ('type' => "subdivision", 'id' => 'a.id')
)
result
SELECT `a`. `" Subdivision "` as `type`, a.id as id FROM `some_table` a;

You have to mark the static value so that Zend_Db_Select does not quote the value as an identifier using Zend_Db_Expr.
$this->select()
->from(array(
'a' => 'some_table'
), array(
'type' => new Zend_Db_Expr($db->quote('subdivision')),
'id' => 'a.id'
)
);

It's not always obvious but for Laminas it'll look like this
$select->from(['a' => 'some_table'])
->columns([
'id' => 'id',
'type' => new Laminas\Db\Sql\Expression('"subdivision"')
]);

Related

Handle Request: ChoicesType

So I have this ChoiceType Form that will sort the items:
$sort = $this->createForm(ChoiceType::class, NULL, array(
'choices' => array(
'...' => 'default',
'A-Z' => 'title_up',
'Z-A' => 'title_down',
'Price low to high' => 'price_up',
'Price high to low' => 'price_down',
),
));
I want to use the Choices so that when one of them is selected from the dropdown menu will do this: $products = "SELECT a FROM AppBundle:Product a ORDER BY a.title ASC".
I tried this:
$sort->handleRequest($request);
if($sort->isSubmitted() && $sort->isValid()) {
if (isset($default)) {
$products = "SELECT a FROM AppBundle:Product a ORDER BY a.title ASC";
return $this->render('AppBundle:main:index.html.twig', array('products' => $products, ));
}
}
But $default is not working, since is not defined. I dont know how to access the choices, so I can pass them to an if statement.
I think you need to write something like this:
$sort = $this->createFormBuilder()
->setAction($this->generateUrl('your_process_route_here'))
->setMethod('POST')
->add('select', ChoiceType::class, [
'placeholder' => 'Please select',
'choices' => [
'...' => 'default',
'A-Z' => 'title_up',
'Z-A' => 'title_down',
'Price low to high' => 'price_up',
'Price high to low' => 'price_down',
]
])
To get the value inside the <select> element:
$select = $request->request->get('select'); // this will contain whatever value you've selected from the dropdown
Check if the value is what you expect, and then create the query:
if ('default' == $select){ // or you can use a switch
// create a custom method inside your Repository class containing the SELECT, and call it here
}
That select from ->add('select', ...) will be the name attribute for your <select> html element.

NativeQuery to queryBuilder impossible but i need querybuilder for my form entity type

I have a sql query too complex to translate in QueryBuilder. It works by nativequery. The problem is that my entity type asks me a QueryBuilder not a query. Do you have a solution?
This is my query :
SELECT v.* FROM vat_rates v
INNER JOIN (
SELECT vr.code as
code2 , MAX(vr.dateIn) AS dateIn2
FROM vat_rates vr
WHERE vr.country = :country AND vr.dateIn = :dateIn
GROUP BY code )
AS max ON code2 = v.code AND dateIn =
v.dateIn;
And in my form type :
$builder->add('vat_rates', 'entity', array(
'label' => 'item_vat_rate',
'class' => 'MyInvoicingBundle:VatRate',
'query_builder' => function(VatRateRepository $cr) use ($options) {
return $cr->getListVatRate($options['country'],$options['dateIn']);
},
'attr' => array('class' => 'form-control'),
))
Thanks you and sorry for my bad english.

Magento Collection with nested "OR" "AND" Filter

i am try to filter my collection which have nest "or" "and" filter
where t_id & t_code both are attributes
$collection->addAttributeToFilter(
array(
array(
'attribute' => 't_id',
'null' => true
),
array(
'attribute' => 't_id',
'finset' => $allowedtId
)
)
);
$collection->addAttributeToFilter(
array(
array(
'attribute' => 't_code',
'null' => true
),
array(
'attribute' => 't_code',
'finset' => $tAttCode
)
)
);
here is the sql of query
WHERE ((at_t_id.value IS NULL) OR (FIND_IN_SET('', at_t_id.value))) AND ((at_t_code.value IS NULL) OR (FIND_IN_SET('2348', at_t_code.value)))
but i am trying to get something like this
WHERE (at_t_id.value IS NULL)
OR
(
(FIND_IN_SET('1245', at_t_id.value))
AND
(
(at_t_code.value IS NULL)
OR
(FIND_IN_SET('2348', at_t_code.value))
)
)
Is there any way this can be achieved?

Get data from one table with foreign_table and update to another in TCA

I'm modifying an image upload extension and I'm working on a feature where images can be placed in categories.
Right now the categories are listed in a select field that uses foreign_table to get the categories from a table (called tx_gallery_categories) and when saved the category Id (the value in the option field) is saved to table called tx_gallery_items.
But that column is no longer needed (i was wrong the first time). Depending on what category you choose I want TCA to update a table called tx_gallery_itemsCategory and set the categoryId where itemId is equal to the saved images uid
Here is the TCA (i have removed all other columns beside categoryId) and categoryId is the one I want to move out from this, I think, and in to it's own TCA which is connected to tx_gallery_itemsCategory:
$TCA["tx_gallery_items"] = array (
"ctrl" => $TCA["tx_gallery_items"]["ctrl"],
"interface" => array (
"showRecordFieldList" => "hidden,oid,filename, videoembedcode,caption"
),
"feInterface" => $TCA["tx_gallery_items"]["feInterface"],
"columns" => array (
"categoryId" => Array (
"exclude" => 1,
"label" => "LLL:EXT:gc_gallery/locallang_db.xml:tx_gallery_items.categories",
"config" => Array (
"type" => "select",
"foreign_table" => "tx_gallery_categories",
// "foreign_table_where" => " true"
// "itemsProcFunc" => "tx_gallery_getImageCategories->getCategories"
// 'default' => '123'
)
),
),
"types" => array (
"0" => array("showitem" => "hidden, oid, filename, categoryId, videoembedcode, caption, linkpid")
)
);
$TCA["tx_gallery_categories"] = array (
"ctrl" => $TCA["tx_gallery_categories"]["ctrl"],
"interface" => array (
"showRecordFieldList" => "categoryTitle"
),
"feInterface" => $TCA["tx_gallery_categories"]["feInterface"],
"columns" => array (
"categoryTitle" => Array (
"exclude" => 0,
"label" => "LLL:EXT:gc_gallery/locallang_db.xml:tx_gallery_items.categories",
"config" => Array (
"type" => "text",
"cols" => "30",
"rows" => "5",
)
)
),
"types" => array (
"0" => array("showitem" => "categoryTitle")
)
);
But instead of it working like that I want to save the images uid from tx_gallery_items to another table called tx_gallery_itemsCategory which is a many to many table between tx_gallery_items and tx_gallery_categories
Here are the tables:
tx_gallery_items:
uid | pid | ... (and many more but only uid is relevant)
432 | 34 | ...
tx_gallery_itemsCategory:
id | itemId | categoryId
1 | 432 | 1
tx_gallery_categories:
uid | pid | categoryTitle
1 | 34 | example category
And here is the ext_tables.php
$TCA["tx_gallery_items"] = array (
"ctrl" => array (
'title' => 'LLL:EXT:gc_gallery/locallang_db.xml:tx_gallery_items',
'label' => 'filename',
'tstamp' => 'tstamp',
'crdate' => 'crdate',
'cruser_id' => 'cruser_id',
'sortby' => 'sorting',
'delete' => 'deleted',
'enablecolumns' => array (
'disabled' => 'hidden',
),
'dynamicConfigFile' => t3lib_extMgm::extPath($_EXTKEY).'tca.php',
'iconfile' => t3lib_extMgm::extRelPath($_EXTKEY).'icon_tx_gallery_items.gif',
),
"feInterface" => array (
"fe_admin_fieldList" => "hidden, oid, filename, category, videoembedcode, caption, linkpid, categoryId",
)
);
$TCA["tx_gallery_categories"] = array (
"ctrl" => array (
'title' => 'LLL:EXT:gc_gallery/locallang_db.xml:tx_gallery_items',
'label' => 'categoryTitle',
'tstamp' => 'tstamp',
'sortby' => 'sorting',
'delete' => 'deleted',
// 'enablecolumns' => array (
// 'disabled' => 'hidden',
// ),
'dynamicConfigFile' => t3lib_extMgm::extPath($_EXTKEY).'tca.php',
'iconfile' => t3lib_extMgm::extRelPath($_EXTKEY).'icon_tx_gallery_items.gif',
),
// "feInterface" => array (
// "fe_admin_fieldList" => "uid, pid, categoryTitle, categoryDescription, tstamp, sorting, deleted, hidden, categorySlug",
// )
);
So my question is (i think) how can I get the uid from the current image the user is editing and save it to another table.
This is my first try with TCA and I'm very confused on how all this is connected.
I hope that anyone knows this better than me :)
Thanks.
There is the hook concept implemented in the tcemain component. There is one called processDatamap_postProcessFieldArray which is invoked when any record is saved in the backend. Thus, you can check whether it's "yours" and do your other queries or whatever you want to change.
There is an example of how to use this feature. Although it is pretty old, it should still be working that way.

Ordering in find()

I have a model Org.
In a model Org I have $hasAndBelongsToMany = array('Patient','Project','Category');
In orgs controller action view I get data from database with
$this->set('org', $this->Org->find('first',array('condition'=> array('id'=>$id))));
Result is ok but I want change the order.
I want order result Patient by name, Project by name and Category by id.
How use ORDER BY for Patient,Project,Category in find() ?
Assuming this is Cake, the ordering of associated models should be done in the model when you declare the HABTM relationship:
<?php
class Org extends AppModel {
var $hasAndBelongsToMany = array(
'Patient' => array(
'order' => array(
'Patient.name ASC'
)
),
'Project' => array(
'order' => array(
'Project.name ASC'
)
),
'Category' => array(
'order' => array(
'Category.id ASC'
)
)
);
}
?>
If you're wanting to sort the actual result set by the associated models you can pass an order array to the find method:
<?php
$org = $this->Org->find('all', array(
'conditions' => array(
'Org.column_name' => $yourVar
),
'order' => array(
'Patient.name ASC',
'Project.name ASC',
'Category.id ASC'
)
));
?>
If you're doing a find first by a unique field (like an ID) you can use the built in findByFIELDNAME helper:
<?php
$org = $this->Org->findById($id);
$this->set(compact(array('org')));