Query with orderings causing empty result - Extbase 6.2 - typo3

I made an extbase extension and want to list my appointments ordered first by startDate and for those appointments that are on the same day I want to order them by the last name of the customer.
In my repository I made following working query:
public function findAppointmentsForList($future) {
$curtime = time();
$query = $this->createQuery();
$constraints = array();
if ($future !== NULL) {
$constraints[] = ($future) ?
$query->greaterThanOrEqual('startDate', $curtime) :
$query->lessThan('startDate', $curtime);
}
if ($constraints) {
$query->matching($query->logicalAnd($constraints));
} else {}
$orderings = array(
'startDate' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
// 'customer.lastName' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
);
$query->setOrderings($orderings);
return $query->execute();
}
It returns me some appointments so I assume it's working.
If I then uncomment the line 'customer.lastName... it returns 0 appointments.
What's going on? It's just the ordering, it can't possibly make the query smaller...
I don't even get any errors - I tried it with an invalid property for example and it gave me an error, so the property name is correct too.
And I debugged the working query and the last names in those customer objects where there.
This is my appointment model entry:
/**
* customer
*
* #var \vendor\extension\Domain\Model\Customer
*/
protected $customer = NULL;
And this is the TCA corresponding to it:
'customer' => array(
'exclude' => 1,
'label' => 'LLL:EXT:extension/Resources/Private/Language/locallang_db.xlf:tx_extension_domain_model_appointment.customer',
'config' => array(
'type' => 'select',
'foreign_table' => 'fe_users',
'minitems' => 0,
'maxitems' => 1,
),
),
EDIT: It's working now...but unfortunately I don't know why, changed too much in the meantime which I thought had nothing to do with this. One thing that might've affected this: startDate is of type Date and I noticed the query didn't filter it right so after I changed the curtime to new \DateTime('midnight') it was filtering correctly.

When you use related models in a query as part of the matching or orderBy then the query will be build with joins to the related tables. That means, that the result is actually smaller and will not include appointments without customers.
The strange thing is that you see the last names when you debug it, otherwise i would assume that you have some configuration errors in the TCA. Can you provide the TCA code from the apointment table and may be its model?

Related

After TYPO3 Update from 6.2 to 7.6 still errors in TCA?

I've made an update from TYPO3 CMS 6.2 to TYPO3 CMS 7.6.16. After a few problems with other extensions (tx_newsand third party ext.) and the changes in the TCA. Everything works fine after import live-dump ...
Upgrade wizard / Database compare
Update reference Index
Flush Cache and empty typo3temp
Deactivate and reactivate the Extensions with problems
Everything? Unfortunately, no. The extension doesn't work. I don't written the extension by myself. If I try to add a new data record in backend with this ext., I'll get this error:
An item in field form of table tx_blah_domain_model_job is not an array as expected
But the database comprare is finished. All tables are correct?!
Where's the problem? I know it's hard to analyze this without source code. There's a database field wrong, but why? It's the same database like before?
Where's the fault .. ext_tables.php or still sth. in TCA is wrong? I really need a tip .. its frustrating ..
EDIT: sys_log entry
Core: Exception handler (WEB): Uncaught TYPO3 Exception: #1439288036: An item in field form of table tx_blah_domain_model_job is not an array as expected | UnexpectedValueException thrown in file /typo3_src/typo3_src-7.6.16/typo3/sysext/backend/Classes/Form/FormDataProvider/AbstractItemProvider.php in line 1264.
EDIT 2: I think, there must be sth. in typo3conf/ext/blah/Configuration/TCA/tx_blah_domain_model_job.php
see TCA source code
and that's in line 1264
/**
* Sanitize incoming item array
*
* Used by TcaSelectItems and TcaSelectTreeItems data providers
*
* #param mixed $itemArray
* #param string $tableName
* #param string $fieldName
* #throws \UnexpectedValueException
* #return array
*/
public function sanitizeItemArray($itemArray, $tableName, $fieldName)
{
if (!is_array($itemArray)) {
$itemArray = [];
}
foreach ($itemArray as $item) {
if (!is_array($item)) {
throw new \UnexpectedValueException(
'An item in field ' . $fieldName . ' of table ' . $tableName . ' is not an array as expected',
1439288036
);
}
}
return $itemArray;
}
aTry to use this in the TCA tx_imappointments_domain_model_job.php
'form' => array(
'exclude' => 1,
'label' => 'LLL:EXT:im_appointments/Resources/Private/Language/locallang_db.xlf:tx_imappointments_domain_model_job.form',
'config' => array(
'type' => 'select',
'renderType' => 'selectSingle',
'items' => array(array('', 0)),
'foreign_table' => 'pages',
'foreign_table_where' => ' AND pages.pid = 293',
'minitems' => 0,
'maxitems' => 1,
),
),
'items' in 'form' has to be a array how your error message said:
https://docs.typo3.org/typo3cms/TCAReference/ColumnsConfig/Type/Select.html#items

Sonata Admin MongoDB DataGrid filter

Situation
We are running Symfony 2.8 and the latest version of Sonata Admin along with Mongo as a data store. Please consider the following object which has been simplified for the sake of this question; it does work.
class Entry
{
/* #ID */
protected $id;
/* #String */
protected $type;
/* #String */
protected $content;
}
With the above, there will be lots of entries and from the Admin itself, we would like to filter by type.
Here's an example of the dataset
Problem
We can't create a set of selectable filters in the dataGrid function which are UNIQUE for type.
Attempts
Note that where needed, the EntryRepository is included as a namespace at the start of the file
NUMBER 1
With the below, we get the type duplicated many times
->add('type', null, array(), 'document', array(
'expanded' => true,
'class' => 'Application:Entry',
'query_builder' => function(EntryRepository $dr) {
return $dr->createQueryBuilder();
}
))
NUMBER 2
With the below, we get a 500 error with only the message "string". I think this is because when using distinct, Mongo prepares a set of arrays instead of unexecuted QueryBuilder object?
->add('type', null, array(), 'document', array(
'expanded' => true,
'class' => 'Application:Entry',
'query_builder' => function(Entryepository $dr) {
return $dr->createQueryBuilder()
->distinct('type');
}
))
NUMBER 3
The attempt below is to use Map reduce to perform the equivalent of an SQL "GROUP BY" however, the same STRING error as above is provided.
->add('type', '', array(), 'document', array(
'expanded' => true,
'class' => 'Application:Entry',
'query_builder' => function(EntryRepository $dr) {
return $dr->createQueryBuilder()
->group(array(), array('type'))
->reduce('function (obj, prev) { prev.type; }');
}
))
CRUDE WORKAROUND...discouraged
The below is a demonstration using the filter (as listed in the Sonata documentation) and it DOES work...for one type at a time.
->add('type', 'doctrine_mongo_callback', array(
'callback' => function($queryBuilder, $alias, $field, $value) {
if (!$value || $value['value'] == false) {
return true;
}
$queryBuilder
->field('type')->equals('fds');
return true;
},
'field_type' => 'checkbox'
))
Taken this approach, I think I'd have to go ahead and query the whole dataset getting the distinct values for type and then loop around each constructing the filter. This would work but be horribly messy.
QUESTION
What is the "best practise" way of performing this without turning the code in to a ruddy mess? Putting the query in a repository will still go and create a similar effect?
Thanks for reading
I'm posting the answer for anyone else who may be facing the same issue. I realised I was approaching the situation from the wrong angle and instead of any of the above, did the following
/* Prepare the options to be used in the filter */
$tagRepo = $this->getConfigurationPool()->getContainer()->get('repository.tag');
$types = $tagRepo->findTypes();
$choices = array();
/* Create a simple choice compatible array */
foreach ($types as $type) { $choices[$type] = $type; }
/* In the actial filter itself */
$datagridMapper
->add('type', 'doctrine_mongo_choice', array(), 'choice',
array('choices' => $choices))

TYPO3 Extbase: How to sort child objects

I have an Extbase Model Article and a 1:n Relation Product. In Article TCA i have an inline field configuered. In my Article Template I want to display all related Products. These are oredered by uid. How can i change the ordering of the child objects to the field sorting to be able to manually sort them. ( In the backend form the sorting is possible, only diplaying them sorted by field sorting is not possible )
thanks,
Lukas
The easiest way to sort child elements in a object storage is to manipulate the TCA.
Example:
$TCA['tx_myext_domain_model_ordering'] = array(
...
'columns' => array(
'services' => array(
'exclude' => 0,
'label' => 'LLL:EXT:myext/Resources/Private/Language/locallang_db.xml:tx_myext_domain_model_ordering.services',
'config' => array(
'type' => 'inline',
'foreign_table' => 'tx_myext_domain_model_service',
'foreign_field' => 'ordering',
'foreign_sortby' => 'sorting',
'maxitems' => 9999,
'appearance' => array(
'collapseAll' => 0,
'levelLinksPosition' => 'top',
'showSynchronizationLink' => 1,
'showPossibleLocalizationRecords' => 1,
'showAllLocalizationLink' => 1
),
),
),
),
...
);
With the filed 'foreign_sortby' => 'sorting', you can easily say on which field the childs should be ordered.
Using the dot notation you can sort by properties of subobjects:
$query->setOrderings(array(
'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
'subobject.sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
));
edit: Caught me off guard, I think I read your question wrong. Yes, this sorts by child objet, but it applies that to the parent object, of course. If you want to sort the child objects themselves, you have to set that sorting in the repository of the child object, as #Urs explained.
Here's how I do it, in the repository class:
class ItemRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
/**
* http://www.typo3.net/forum/thematik/zeige/thema/114160/?show=1
* Returns items of this repository, sorted by sorting
*
* #return array An array of objects, empty if no objects found
* #api
*/
public function findAll() {
$orderings = array(
'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
);
$query = $this->createQuery();
$query->setOrderings($orderings);
$query->getQuerySettings()->setRespectSysLanguage(FALSE);
$query->getQuerySettings()->setSysLanguageUid(0);
$result = $query->execute();
return $result;
}
}
I dont't really like the idea of doing basic sorting inside the view (mvc).
The Downside of most Database abstraction Layers ist, that you are quite often restricted to either mixup mvc or have a (sometimes slightly) lower performance.
I am at the same point right now. I am thinking about retrieving the parents (with their children attached). Then go into every single parent and get children for this parent in a sorted way.
public function findAllSorted() {
$query = $this->createQuery();
/* sort parents */
$query->setOrderings(array('name' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));
$parents = $query->execute();
foreach($parents as $parent){
/* get children for every parent */
$children = $this->getChildrenSorted($parent);
// add children to parent TBD
}
/* Debug output */
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($parents);
}
public function getChildrenSorted(\mynamespace\ $parent) {
/* Generate an object manager and use dependency injection to retrieve children repository */
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Extbase\Object\ObjectManager');
$childrenRepository= $objectManager->get('MYNAME\Extname\Domain\Repository\Repository');
$children = $versionRepository->findSortedByName($parent);
return $children;
}
The function findSortedByName inside the children repos is using
$query->matching($query->equals('parent', $parent));
to retrieve only children of this parent and
$query->setOrderings(array('name' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));
to be able to give them back in an ordered way.
Warning: Depending on the size and quantity of all retrieved objects, this way might arise a huge performance issue.

Cakephp find order by an associated model

After spending few hours to find a solution for the following issue I´m hoping you can help me.
I have two tables:
Event hasmany Appointments and Appointment belongsto Event
Now I perform in my EventsController:
$this->Event->find('all')
Now I want to order the Events by Appointments. So that the Event with the earliest Appointment is first in the array and so on. I´ve tried many ways, with Containable, Joins, Grouping, Subselects but nothing works.
To perform the find('all') on $this->Event->Appointment is not a solution due to the fact that I want every Event just ones.
Edit #1:
My only solution so far was
$events = $this->Event->find('all', array(
'joins' => array(
array(
'table' => '(SELECT event_id, MIN(start) start FROM appointments GROUP BY event_id)',
'alias' => 'Appointment',
'type' => 'LEFT',
'conditions' => array(
'Event.id = Appointment.event_id'
)
)
),
'order' => array(
'Appointment.start ASC'
)
));
But this is not the best solution!
An option is to create a virtual field representing the earliest appointment date that you can sort by
Put this in your Event model:
function __construct($id = false, $table = null, $ds = null) {
$this->virtualFields['firstappt'] = 'SELECT MIN(Appointment.created) FROM appointments AS Appointment WHERE Appointment.event_id = Event.id';
parent::__construct($id, $table, $ds);
}
You should be able to use this code in your controller:
$this->Event->find('all',array('order' => array('firstappt')));

Zend_Validate_Db_RecordExists against 2 fields

I usualy use Zend_Validate_Db_RecordExists to update or insert a record. This works fine with one field to check against. How to do it if you have two fields to check?
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => $this->_name,
'field' => 'id_sector,day_of_week'
)
);
if ($validator->isValid($fields_values['id_sector'],$fields_values['day_of_week'])){
//true
}
I tried it with an array and comma separated list, nothing works... Any help is welcome.
Regards
Andrea
To do this you would have to extend the Zend_Validate_Db_RecordExists class.
It doesn't currently know how to check for the existence of more than one field.
You could just use two different validator instances to check the two fields separately. This is the only work around that I can see right now besides extending it.
If you choose to extend it then you'll have to find some way of passing in all the fields to the constructor ( array seems like a good choice ), and then you'll have to dig into the method that creates the sql query. In this method you'll have to loop over the array of fields that were passed in to the constructor.
You should look into using the exclude parameter. Something like this should do what you want:
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => $this->_name,
'field' => 'id_sector',
'exclude' => array(
'field' => 'day_of_week',
'value' => $fields_values['day_of_week']
)
);
The exclude field will effectively add to the automatically generated WHERE part to create something equivalent to this:
WHERE `id_sector` = $fields_values['id_sector'] AND `day_of_week` = $fields_values['day_of_week']
Its kind of a hack in that we're using it for the opposite of what it was intended, but its working for me similar to this (I'm using it with Db_NoRecordExists).
Source: Zend_Validate_Db_NoRecordExists example
Sorry for the late reply.
The best option that worked for me is this:
// create an instance of the Zend_Validate_Db_RecordExists class
// pass in the database table name and the first field (as usual)...
$validator = new Zend_Validate_Db_RecordExists(array(
'table' => 'tablename',
'field' => 'first_field'
));
// reset the where clause used by Zend_Validate_Db_RecordExists
$validator->getSelect()->reset('where');
// set again the first field and the second field.
// :value is a named parameter that will be substituted
// by the value passed to the isValid method
$validator->getSelect()->where('first_field = ?', $first_field);
$validator->getSelect()->where('second_field = :value', $second_field);
// add your new record exist based on 2 fields validator to your element.
$element = new Zend_Form_Element_Text('element');
$element->addValidator($validator);
// add the validated element to the form.
$form->addElement($element);
I hope that will help someone :)
Although, I would strongly recommend a neater solution which would be to extend the Zend_Validate_Db_RecordExists class with the above code.
Enjoy!!
Rosario
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
'validators' => array('EmailAddress', $obj= new Zend_Validate_Db_NoRecordExists(array('adapter'=>$dbAdapter,
'field'=>'email',
'table'=>'user',
'exclude'=>array('field'=>'email','value'=>$this->_options['email'], 'field'=>'is_deleted', 'value'=>'1')
))),
For those using Zend 2, If you want to check if user with given id and email exists in table users, It is possible this way.
First, you create the select object that will be use as parameter for the Zend\Validator\Db\RecordExists object
$select = new Zend\Db\Sql\Select();
$select->from('users')
->where->equalTo('id', $user_id)
->where->equalTo('email', $email);
Now, create RecordExists object and check the existence this way
$validator = new Zend\Validator\Db\RecordExists($select);
$validator->setAdapter($dbAdapter);
if ($validator->isValid($username)) {
echo 'This user is valid';
} else {
//get and display errors
$messages = $validator->getMessages();
foreach ($messages as $message) {
echo "$message\n";
}
}
This sample is from ZF2 official doc
You can use the 'exclude' in this parameter pass the second clause that you want to filter through.
$clause = 'table.field2 = value';
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => 'table',
'field' => 'field1',
'exclude' => $clause
)
);
if ($validator->isValid('value') {
true;
}
I am using zend framework v.3 and validation via InputFilter(), it uses same validation rules as zend framework 2.
In my case I need to check, if location exists in db (by 'id' field) and has needed company's id ('company_id' field).
I implemented it in next way:
$clause = new Operator('company_id', Operator::OP_EQ, $companyId);
$inputFilter->add([
'name' => 'location_id',
'required' => false,
'filters' => [
['name' => 'StringTrim'],
['name' => 'ToInt'],
],
'validators' => [
[
'name' => 'Int',
],
[
'name' => 'dbRecordExists',
'options' => [
'adapter' => $dbAdapterCore,
'table' => 'locations',
'field' => 'id',
'exclude' => $clause,
'messages' => [
'noRecordFound' => "Location does not exist.",
],
]
],
],
]);
In this case validation will pass, only if 'locations' table has item with columns id == $value and company_id == $companyId, like next:
select * from location where id = ? AND company_id = ?