Asked  7 Months ago    Answers:  5   Viewed   31 times

I want to get an array of values from the id column of the Auction table. If this was a raw SQL I would write:

SELECT id FROM auction

But when I do this in Doctrine and execute:

$em->createQuery("SELECT a.id FROM Auction a")->getScalarResult(); 

I get an array like this:

array(
    array('id' => 1),
    array('id' => 2),
)

Instead, i'd like to get an array like this:

array(
    1,
    2
)

How can I do that using Doctrine?

 Answers

71

PHP < 5.5

You can use array_map, and since you only have on item per array, you can elegantly use 'current' as callback, instead of writing a closure.

$result = $em->createQuery("SELECT a.id FROM Auction a")->getScalarResult();
$ids = array_map('current', $result);

See Petr Sobotka's answer below for additional info regarding memory usage.

PHP >= 5.5

As jcbwlkr's answered below, the recommended way it to use array_column.

Wednesday, March 31, 2021
 
linjuming
answered 7 Months ago
18

$ app/console help doctrine:fixtures:load

By default Doctrine Data Fixtures uses DELETE statements to drop the existing rows from the database. If you want to use a TRUNCATE statement instead you can use the --purge-with-truncate flag:

./app/console doctrine:fixtures:load --purge-with-truncate

Truncate will reset the auto increments.

UPDATE

The console command is for Symfony, but it should be the same using Doctrine only:

./bin/doctrine orm:fixtures:load --purge-with-truncate

UPDATE #2 for the comment about throwing an exception

If you have foreign keys, you can only reset the AUTO_INCREMENT through regular SQL:

$connection = $this->getEntityManager()->getConnection();
$connection->exec("ALTER TABLE <tablename> AUTO_INCREMENT = 1;");
Saturday, May 29, 2021
 
Asher
answered 5 Months ago
45

You can easily do this with the TryInto trait (which was stabilized in Rust 1.34):

use std::convert::TryInto;

fn pop(barry: &[u8]) -> [u8; 3] {
    barry.try_into().expect("slice with incorrect length")
}

But even better: there is no need to clone/copy your elements! It is actually possible to get a &[u8; 3] from a &[u8]:

fn pop(barry: &[u8]) -> &[u8; 3] {
    barry.try_into().expect("slice with incorrect length")
}

As mentioned in the other answers, you probably don't want to panic if the length of barry is not 3, but instead handle this error gracefully.

This works thanks to these impls of the related trait TryFrom (before Rust 1.47, these only existed for arrays up to length 32):

impl<'_, T, const N: usize> TryFrom<&'_ [T]> for [T; N]
where
    T: Copy, 

impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]

impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
Tuesday, June 1, 2021
 
Akarun
answered 5 Months ago
71

Broadcasting is really good for this:

row_sums = a.sum(axis=1)
new_matrix = a / row_sums[:, numpy.newaxis]

row_sums[:, numpy.newaxis] reshapes row_sums from being (3,) to being (3, 1). When you do a / b, a and b are broadcast against each other.

You can learn more about broadcasting here or even better here.

Thursday, June 3, 2021
 
Tucker
answered 5 Months ago
57

The date() function returns a string. The datetime type works with DateTime objects. So either change the mapping type to string or use DateTime objects.

Thursday, July 29, 2021
 
JustSteveKing
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :