custom query with propel symfony 2 - php

This is my first time using symfony 2. For database integration i am thinking of using propel as I doctrine and annotations seems really difficult for me. But it seems to me that to make a query you have to use propels own functions. I have used codeigniter. In codeigniter I used to send query string and it used to send me data. Is there something similar in propel symfony 2?
Like -
$query = 'select * from table where column1 natural join column2';
$this->db->query($query);

You should look at docs of sf2:
http://symfony.com/doc/current/book/propel.html
If you want to use raw SQL:
$em = $this->getDoctrine()->getEntityManager();
$connection = $em->getConnection();
$statement = $connection->prepare("SELECT something FROM somethingelse");
$statement->execute();
$results = $statement->fetchAll();
Or "propel way":
$connection = Propel::getConnection();
$query = 'SELECT MAX(?) AS max FROM ?';
$statement = $connection->prepareStatement($query);
$statement->setString(1, ArticlePeer::CREATED_AT);
$statement->setString(2, ArticlePeer::TABLE_NAME);
$resultset = $statement->executeQuery();
$resultset->next();
$max = $resultset->getInt('max');

Related

Problems with search query using php and mysql

i just want a direct answer and explanation why my prior query works but the latter does not..
here is the query that works just fine:
$sql = "SELECT * FROM productslist WHERE brand LIKE ?";
and this doesn't work at all and just returns an error:
$sql = 'SELECT * FROM productslist WHERE brand LIKE "%'.$search_string.'%"';
can someone please explain me why the latter query doesn;t work at all?
thanks in advance..
I tested the query with a table on my own DB. worked fine... I used a constant
Try mysqli_real_escape_string:
$search_string = mysqli_real_escape_string($conn, $search_string);
$sql = 'SELECT * FROM productslist WHERE brand LIKE "%'. $search_string .'%"';
Try this with PDO (Edit: Including PDO connection string since he didn't specify if he was using PDO) -
$dbh = new PDO("mysql:hostname=$your_server;dbname=$database_name, $username, $password);
$sql = "SELECT * FROM productslist WHERE brand LIKE :search";
$query = $dbh->prepare($sql);
$query->bindValue(":search", "%$search_string%");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);

Counting the numbers of rows in Zend Framework

In PHP you can use the function mysqli_stmt_num_rows($stmt) to check how much rows there are in the result set.
I was wondering, how to do this if you are using Zend Framework. What I'm trying to achieve is to check if the email existst in the database table: customer.
I have this code.
$db = Zend_Registry::get('db');
$query = "SELECT column1,column2 FROM ". Zim_Properties::getTableName('Customer') ." WHERE email = '".$_POST['email']."'";
$stmt = $db->query($query);
I use Zend Framework 1.8
What I did to solve the problem was the following.
To know more information about the var $stmt I used Zend_Debug::dump($stmt);
It showed:
object(Zend_Db_Statement_Mysqli)#14 (12) {
So I went to the folder "library/Zend/Db/Statement", and opened the file Mysqli.php.
Then I checked every method available in the class and noticed that the method rowCount was the needed one.
At the Controller I called this rowCount function.
$db = Zend_Registry::get('db');
$query = "SELECT column1,column2 FROM ". Zim_Properties::getTableName('Customer') ." WHERE email = '".$_POST['email']."'";
$stmt = $db->query($query);
echo $stmt->rowCount();

How to get results from after stmnt->execute in doctrine2 symfony

I am using this:
$stmt = $this->getEntityManager()->getConnection()->prepare('SELECT * from tbl_users');
$stmt->execute();
How to get the results back in variables:
$stmt->getResults()
is not working
You forgot about fetching result. fetchAll() could help you:
$stmt = $this->getEntityManager()
->getConnection()
->prepare('SELECT * from tbl_users');
$stmt->execute();
$result = $stmt->fetchAll();
If you want to hydrate your TblUsers after fetching data - you can try to to next:
$tblUser = new TblUsers();
$tblUser->fromArray($result);

doctrine native sql query update

I need help. I need to get the result of a update sql query using doctrine 1.2
I googled and I found a way to make a native sql query using doctrine
$preparequerystring = "UPDATE mdw_reportes_operacion SET mmaplicado = 2.742495126705653 WHERE id = 5294;";
$con = Doctrine_Manager::getInstance()->connection();
$st = $con->executeUpdate($preparequerystring,array(false));
print_r($st);
I would like to know how to retrieve some information about the query I'm executing. I've tried with
$result = $st->fetch();
but it returns a 500 internal server error.
What is the right way to retrieve information about the query when performing an UPDATE query?
Why not using the default update from doctrine instead of a row sql ?
$q = Doctrine_Query::create()
->update('MdwReportesOperacion')
->set('mmaplicado', '2.742495126705653')
->where('id = ?', array(5294));
$rows = $q->execute();
echo $rows;
Edit:
I didn't know how to retrieve the result from a raw doctrine query, but you still can use pdo query:
// update the way you want to retrieve database information
$database = sfYaml::load(dirname(__FILE__).'/../config/databases.yml');
$param = $database['all']['doctrine']['param'];
$conn = new PDO($param['dsn'], $param['username'], $param['password']);
// and then perform the query and retrieve the result
$rows = $conn->exec("UPDATE mdw_reportes_operacion SET mmaplicado = 2.742495126705653 WHERE id = 5294;");
echo $rows;

RAW SQL Query with Zend Framework

Is there a way to execute a SQL String as a query in Zend Framework?
I have a string like that:
$sql = "SELECT * FROM testTable WHERE myColumn = 5"
now I want to execute this string directly withput parsing it and creating a Zend_Db_Table_Select object from it "by hand". Or if thats possible create a Zend_Db_Table_Select object from this string, to execute that object.
How can I do that? I didn't find a solution for this in the Zend doc.
If you're creating a Zend_DB object at the start you can create a query using that. Have a look at this entry in the manual : https://framework.zend.com/manual/1.12/en/zend.db.statement.html
$stmt = $db->query(
'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?',
array('goofy', 'FIXED')
);
Or
$sql = 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?';
$stmt = new Zend_Db_Statement_Mysqli($db, $sql);
If you are using tableGateway, you can run your raw SQL query using this statement,
$this->tableGateway->getAdapter()->driver->getConnection()->execute($sql);
where $sql pertains to your raw query. This can be useful for queries that do not have native ZF2 counterpart like TRUNCATE / INSERT SELECT statements.
You can use the same query in Zend format as
$select = db->select()->from(array('t' => 'testTable'))
->$where= $this->getAdapter()->quoteInto('myColumn = ?', $s);
$stmt = $select->query();
$result = $stmt->fetchAll();
Here is an example for ZF1:
$db =Zend_Db_Table_Abstract::getDefaultAdapter();
$sql = "select * from user"
$stmt = $db->query($sql);
$users = $stmt->fetchAll();

Categories