How to query by substring in a MongoDB ODM - mongodb

I have this actual code
public function findCode($code_received)
{
$pellicules =
$this->createQueryBuilder()
->field('code_base')->equals($code_received)
->getQuery()
;
}
But sometimes my code received is inside my code_base, for example:
Code_received = Abata
Code_base = Abata23xiub
I tried to use the in instead of equals but I'm not having success. How could I do it?

Related

Symfony 2.4.1 and Doctrine 2 dates interval query

I have two tables: Empleado and Fichaje in a (1..*) relationship.
I created a query builder for getting fichajes corresponding to IdEmpleado (key) property in Fichaje.
I attempt to filter those Fichajes, but it never works. So I've searched for any clear example of dates in Doctrine for this basic case in vane.
The query result is empty always. No error is thrown.
If I check for IdEmpleado parameter only it gives me all the available records.
The dates interval is the problematic one.
Note: I checked this similar post
Here is the table data, I'm quite convinced of the dates availability.
This is my function:
public function empleadoAction(Request $request){
...
$repository = $em->getRepository('ZkTimeBundle:Fichaje');
$fichajes = $repository->FindByEmpleadoAndDateInterval(
array(
'IdEmpleado' => $workerId,
'FechaInicial' => (new \DateTime('2014-01-10'))->format('Y-m-d'),
'FechaFinal' => (new \DateTime('today'))->format('Y-m-d')
));
...
This is my repository function:
public function FindByEmpleadoAndDateInterval($parameters = array(), $limit = null){
...
$qb = $this->createQueryBuilder('q');
$qb
->where('q.IdEmpleado = :IdEmpleado')
->andWhere('q.Fecha > :FechaInicial')
->andWhere('q.Fecha < :FechaFinal')
->setParameter('IdEmpleado', $parameters['IdEmpleado'])
->setParameter('FechaInicial', $parameters['FechaInicial'])
->setParameter('FechaFinal', $parameters['FechaFinal'])
;
return $qb->getQuery()->execute();
}
Folks, careful with this, have a nice look to the format of dates when you're working with Doctrine 2. The problem was this:
-I've set dates format as: 'Y-M-d', but: 'Ymd' was the correct one (in my particular case).
So, have faith in Doctrine 2 and try every known format (Y-m-d), (Y/m/d), etc. So, you could use dates intervals in these simple ways:
public function findByEmpleadoAndDateInterval($parameters = array(), $limit = null)
{
$qb = $this->createQueryBuilder('q');
$qb->where('q.IdEmpleado = :IdEmpleado')
->andWhere('q.Fecha between :FechaInicial and :FechaFinal')
->setParameters($parameters);
return $qb->getQuery()->execute();
}
OR
public function findByEmpleadoAndDateInterval($parameters = array(), $limit = null)
{
$qb = $this->createQueryBuilder('q');
$qb->where('q.IdEmpleado = :IdEmpleado')
->andWhere('q.Fecha >= :FechaInicial and q.Fecha <= :FechaFinal')
->setParameters($parameters);
return $qb->getQuery()->execute();
}
Maybe, out there, there are more elaborated examples, but this case took me a while to figure it out. Specially because isn't directly accesible online.

Typo3 extbase sql_fetch_assoc error

In Typo3 version 6.1, I have written some custom queries in My extension Repository
for example in file Mytest/Classes/Domain/Repository/MytestRepository.php
class MytestRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
public function myFunction(){
$sql = 'SELECT * FROM some_table ';
$sqlResult = $GLOBALS['TYPO3_DB']->sql_query($sql);
while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($sqlResult)) {
$rowone = $row['rowone'];
}
}
}
And calling that function in controller
$test = $this->MytestRepository->myFunction();
But the issue here is, I am getting error
Fatal error: Call to a member function fetch_assoc() on a non-object in /home/src/typo3_src-6.1.1/typo3/sysext/core/Classes/Database/DatabaseConnection.php on line 1029
Anybody have the solution ?
Thanks in advance.
You can execute custom queries like this:
$query = $this->createQuery();
$query->statement('SELECT * FROM some_table');
$result = $query->execute();
If you don't want to get objects as a result from the query, which i assume looking at your while loop, you can set the following line before executing the query:
$query->getQuerySettings()->setReturnRawQueryResult(TRUE);
With setReturnRawQueryResult you get a plain array as a result.

Zend Db query to select all IDs

How would I write an Zend DB query to select all from the column ID?
So far I have tried:
public function getLatestUserID()
{
$ids = $this->select()
->where('id = ?');
return $ids;
}
But to no avail.
You just want the id column,
You failed to call an execute command.
try:
//assuming you are using a DbTable model
public function getLatestUserID()
{
$ids = $this->fetchAll('id');
return $ids;
}
I would do it like this, because I use the select() object for everything:
public function getLatestUserID()
{
$select = $this->select();
//I'm not sure if $this will work in this contex but you can out the table name
$select->from(array($this), array('id'));
$ids = $this->fetchAll($select);
return $ids;
}
The first two examples should return just the id column of the table, now if you actually want to query for a specific id:
public function getLatestUserID($id)
{
$select = $this->select();
$select->where('id = ?', $id);
//fetchAll() would still work here if we wanted multiple rows returned
//but fetchRow() for one row and fetchRowset() for multiple rows are probably
//more specific for this purpose.
$ids = $this->fetchRow($select);
return $ids;
}
make sure your class containing getLatestUserID does extend Zend_Db_Table_Abstract also :
$ids = $this->select()->where('id = ?'); can't work because where('id = ?'); expects an id value like where('id = ?', $id);
if what you want is the latest inserted row's Id use :
$lastInsertId = $this->getAdapter()->lastInsertId();
(however if you are using an oracle database this will not work and you should use $lastInsertId = $this->getAdapter()->lastSequenceId('USER_TABLE_SEQUENCE'); )

Scalar Value from stored procedure via Entity Framework

I have found a few articles like this one:
http://devtoolshed.com/using-stored-procedures-entity-framework-scalar-return-values
Yet when I take the step to create a function import for a int32 scalar, this is what gets generated:
public ObjectResult<Nullable<global::System.Int32>> MyStoredProcedure(Nullable<global::System.Int32> orderId)
{
ObjectParameter orderIdParameter;
if (orderId.HasValue)
{
orderIdParameter = new ObjectParameter("OrderId", orderId);
}
else
{
orderIdParameter = new ObjectParameter("OrderId", typeof(global::System.Int32));
}
return base.ExecuteFunction<Nullable<global::System.Int32>>("MyStoredProcedure", orderIdParameter);
}
I am able to call the procedure with this, but am not able to get to the underlying scalar:
ObjectResult<int?> result = myEntities.MyProcedure(orderId);
In the code examples I have seen, I should get context.MyProcedure().SingleOrDefault().
Try this:
int? result = myEntities.MyProcedure(orderId).FirstOrDefault();

Symfony form with doctrine table other than getTable()->find() is not working

I get a really anoying error when I try to edit an entry from a table, in tutorial they always use getTable()->find(), but I need to verify that the person logged in is the owner of that entry here what I did:
In the action:
public function executeEdit(sfWebRequest $request)
{
$id = $request->getParameter('id');
$userid = $this->getUser()->getGuardUser()->getId();
$ad = Doctrine_Core::getTable('BambinbazarArticles')->getMyAd($id, $userid);
$this->forward404Unless($ad, sprintf('Object bambinbazar_articles does not exist (%s).', $request->getParameter('id')));
$this->form = new BambinbazarArticlesForm($ad);
}
In the model:
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid);
return $q->execute();
}
I tried it with and without the ->execute(), did doctrine clean, cleared cache, rebuilded model,
Always get the same error 'The "%s" form only accepts a "%s" object.
If I use the Doctrine_Core::getTable('BambinbazarArticles')->find() it work, but of course, i need more than that..
I am becoming crazy over this.
execute() can return multiple rows; effectively you're getting a recordset back, rather than the individual object that your form is expecting. Try fetching a single object, using, e.g.:
return $q->execute()->getFirst();
or
return $q->fetchOne();
Its probably because your query is returning a Doctrine_Collection, not the actual Doctrine_Record youre expecting. Instead of execute use fetchOne.
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid)
->limit(1);
return $q->fetchOne();
}