How to access various tables in a database without creating objects in symfony

I have a table with 3 columns EmployeeNum, JobTitleCode and cost. I created an object for it with the basic functions of CRUD.

EmployeeNum and JobTitleCode are both foreign keys, and when displaying an object, I want to be able to query another table in the database to retrieve EmployeeName from EmployeeNum and JobTitle from JobTitleCode.

In Symfony 1.4, I could just do Doctrine_Core :: getTable ("Employees"). Find ("EmployeeNum") by specifying "Employees" in the schema, but I could not find a similar method in Symfony 2.4.

From the API for getDoctrine (), I did not find similar methods that I could use, and if I build the request as such

createQuery('SELECT a FROM hs_hr_employee a WHERE a.emp_number=1')

I get the error below

[Semantical Error] line 0, col 14 near 'hs_hr_employee': Error: Class 'hs_hr_employee' is not defined

So how are you going to access data from other tables in db?

+3
source share
1 answer

Doctrine always tries to match your results with default objects.

You can use the prepared report without having to provide a comparison of the results:

get connection:

$connection = $em->getConnection();

preparation of instructions and its implementation:

$statement = $connection->prepare(
    'SELECT a FROM hs_hr_employee a WHERE a.emp_number = :emp'
);
$statement->bindValue('emp', $emp);
$statement->execute();

// for SELECT queries 
$result = $statement->fetchAll('EAGER');  // note: !== $connection->fetchAll()!

// for INSERT, UPDATE, DELETE queries
$affected_rows = $statement->rowCount();

This is quite expressive. See Examples below for shorter options.


Alternative:

Use the Source SQL query to display the results.

The extended example shows how to rename columns in an array of results.

// create a result-mapping
$rsm = new ResultSetMapping;
$rsm->addScalarResult('n', 'nickname');
$rsm->addScalarResult('f', 'muchachos');

$query = $em->createNativeQuery(
    '
     SELECT 
        users.name as n
        COUNT(user.friends) as f
     WHERE
        users.name = :username_parameter
     FROM
        user_table_name users
    ',
    $rsm
);
$query->setParameter('username_parameter', $username); 

$result = $query->getSingleResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);

// example: $username = 'nifr'
// array => [ 
//   'nickname'  => 'nifr'
//   'muchachos' => 3919410
// ]

: ( + )

// for SELECT queries
$result = $connection->executeQuery(
    'SELECT a FROM hs_hr_employee a WHERE a.emp_number = ?'
    array($emp)
);

//for INSERT, UPDATE, DELETE queries
$affected_rows = $connection->executeUpdate(
    'DELETE FROM hs_hr_employee a WHERE a.emp_number = ?',
    array($emp)
);

// fetch all into array ( <numeric-index> => <associative-array-entry> )
$connection->fetchAll(
    'SELECT a FROM hs_hr_employee a WHERE a.emp_number = ?',
    array($emp)
);
// fetch only first-row of result-set as associative array
$connection->fetchAssoc(
   'SELECT a FROM hs_hr_employee a WHERE a.emp_number = ?',
    array($emp)
);
// fetch only first-row of result-set as array with numeric indexes
$connection->fetchArray(
    'SELECT a FROM hs_hr_employee a WHERE a.emp_number = ?',
    array($emp)
);

( INSERT, UPDATE DELETE)

// DELETE FROM user WHERE id = ? (1)
$conn->delete('user', array('id' => 1));

// INSERT INTO user (username) VALUES (?) (nifr)
$conn->insert('user', array('username' => 'nifr'));

// UPDATE user (username) VALUES (?) WHERE id = ? (nifr, 1)
$conn->update('user', array('username' => 'nifr'), array('id' => 1));

:

+3