QueryBuilder lastInsertId() - typo3

I would like to have the last UID of the entry. For mysql, it is "insertId()". How does that work at Extbase?
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder
->insert($table)
->values([
'test_id' => 1,
'test2_id' => 2,
]);
$queryBuilder->execute();
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table2);
$queryBuilder
->insert($table2)
->values([
'uid_local' => ?, // uid from $table
'uid_foreign' => 1
]);
$queryBuilder->execute();

You can get the last insert id with $queryBuilder->getConnection()->lastInsertId();
So that would make it:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder
->insert($table)
->values([
'test_id' => 1,
'test2_id' => 2,
]);
$queryBuilder->execute();
$tableUid = $queryBuilder->getConnection()->lastInsertId();
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table2);
$queryBuilder
->insert($table2)
->values([
'uid_local' => $tableUid,
'uid_foreign' => 1
]);
$queryBuilder->execute();

I'm a bit unsure if it's available when using the QueryBuilder for inserting new records, but the migration guide in TYPO3 Docs explain how you migrate the sql_insert_id() method in the new Doctrine Database Layer
https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Database/Migration/Index.html#sql-insert-id
Hint:
(int)$databaseConnectionForPages->lastInsertId('pages');

Related

How can I filter through my meta query via acf fields?

I have a group of posts that can select a custom field 'Partner Content'. These posts then select their post author. The post author is a custom post type that has a relationship field that selects the 'Partner Company' associated with the post author.
I am now trying to set up a single-partner_company.php template that displays all posts associated with the specific company. My meta query looks like this:
<?php
$current_company = $post;
$comp_id = $current_company->ID;
$comp_name = $current_company->post_title;
?>
<?php
$current_company = $post;
$comp_id = $current_company->ID;
$comp_name = $current_company->post_title;
?>
<?php
$args = array(
'numberposts' => -1,
'fields' => 'ids',
'post_type' => 'partner_authors',
'meta_query' => array(
'key' => 'author_company',
'value' => $current_company
)
);
$authors = get_posts( $args );
$post_args = array(
'fields' => 'ids',
'numberposts' => 3,
'post_type' => 'post',
'meta_query' => array(
'key' => 'post_author',
'value' => $authors
)
);
$partner_posts = get_posts( $post_args );
$count = 0;
?>
neither array is working for me. what am I doing wrong?

enablecolumns and QueryBuilder in TYPO3 9.5

I have a question about enablecolumns and QueryBuilder in TYPO3 9.5:
In the TCA I defined the enablecolumns:
'ctrl' => [
'title' => '....'
'tstamp' => 'tstamp',
'crdate' => 'crdate',
'versioningWS' => 2,
'versioning_followPages' => true,
'origUid' => 't3_origuid',
'languageField' => 'sys_language_uid',
'transOrigPointerField' => 'l18n_parent',
'transOrigDiffSourceField' => 'l18n_diffsource',
'delete' => 'deleted',
'enablecolumns' => [
'disabled' => 'hidden',
'fe_group' => 'fe_group',
],
]
In the repository I create a custom Query:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_myext_domain_model_table');
$result = $queryBuilder
->select('*')
->from('tx_myext_domain_model_table')
->where($where)
->execute();
If I debug the SQL with
$queryBuilder->getSQL();
I see the deleted, and hidden conditions but no fe_group
If I add
$queryBuilder
->getRestrictions()
->removeAll()
->add(GeneralUtility::makeInstance(FrontendGroupRestriction::class))
->add(GeneralUtility::makeInstance(DeletedRestriction::class))
->add(GeneralUtility::makeInstance(HiddenRestriction::class));
before the execution of the query, the fe_groups condition ist added.
What am I missing?
Thank you
Christian
This is because the QueryBuilder by default uses the DefaultRestrictionContainer which adds only the following restrictions:
protected $defaultRestrictionTypes = [
DeletedRestriction::class,
HiddenRestriction::class,
StartTimeRestriction::class,
EndTimeRestriction::class
];
References:
https://github.com/TYPO3/TYPO3.CMS/blob/9.5/typo3/sysext/core/Classes/Database/Query/QueryBuilder.php#L86
https://github.com/TYPO3/TYPO3.CMS/blob/9.5/typo3/sysext/core/Classes/Database/Query/Restriction/DefaultRestrictionContainer.php#L28-L33
What you're probably looking for is the FrontendRestrictionContainer which uses the following default restrictions:
protected $defaultRestrictionTypes = [
DeletedRestriction::class,
FrontendWorkspaceRestriction::class,
HiddenRestriction::class,
StartTimeRestriction::class,
EndTimeRestriction::class,
FrontendGroupRestriction::class,
];
Reference: https://github.com/TYPO3/TYPO3.CMS/blob/9.5/typo3/sysext/core/Classes/Database/Query/Restriction/FrontendRestrictionContainer.php#L33-L40
A possible solution would be to use this container instead of the default one:
$container = GeneralUtility::makeInstance(FrontendRestrictionContainer::class);
$queryBuilder->setRestrictions($container);

How to use where/whereDate in mongodb aggregate functions using Jessengers package in Laravel?

How to use where/whereDate in aggregate mongodb query ?
I am using aggregate query in laravel and jessenger-mongodb to group by fields.
The problem here is, I need to add a condition to fetch grouped data of a particular date i.e created_at.
Following code worked perfectly if I do not use pass $date i.e $date = null:
public static function getUsersByTypeLogin($date = null)
{
$q = self::raw()->aggregate(
[
array(
'$match' => array(
'type' => self::LOGIN,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]
);
if(true == $date){
$q = $q->whereDate('created_at','=',$date);
}
return $q;
}
but If I pass $date it returns an error, it says:
message: "Call to undefined method MongoDB\Driver\Cursor::whereDate()"
$date is a Carbon object e.g:
Carbon #1549843200 {#791
date: 2019-02-11 00:00:00.0 +00:00
}
Please help me in this regards.
Try this.
$q = self::raw(function($collection) use($date) {
return $collection->aggregate([
array(
'$match' => array(
'type' => self::LOGIN,
'created_date' => $date,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]);
});

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.

Cakephp aggregation query (MongoDB)

Using ichikaway-cakephp I am trying to convert following query (running fine in php) to cakephp
In cakephp it returns empty array
Core PHP
$out = $collection->aggregate(array(
array('$unwind' => '$as'),
array(
'$group' => array(
'_id' => array('as'=>'$as'),
'count' => array('$sum' => 1)
)
)
));
Cakephp
$conditions=array('aggregate'=>array(
array('$unwind' => '$as'),
array(
'$group' => array(
'_id' => array('as'=>'$as'),
'count' => array('$sum' => 1)
)
)
));
$results = $this->Post->find('all',array('conditions'=>$conditions));
I am unable to find aggrgation framework function in test cases
So far only this commit talks about aggregation.
$params = array(
array('$unwind' => '$as'),
array(
'$group' => array(
'_id' => array('as'=>'$as'),
'count' => array('$sum' => 1)
)
)
));
$mongo = $this->Post->getDataSource();
$mongoCollectionObject = $mongo->getMongoCollection($this->Post);
$results = $mongoCollectionObject->aggregate($params);