So I am trying to accomplish something like this:
SELECT * FROM table WHERE status_id IN (1,3,4);
using Zend_Db_Select... can't find how to do it :( Is it at all possible?
you can also use it like this:
$data = array(1,3,4);
$select->where('status_id IN(?)', $data);
you dont need to implode array, and it's safer
The first answer probably works in ZF1 but it doesn't work in Zend Framework 2:
$data = array(1,3,4);
$select->where('status_id IN(?)', $data);
In case the Zend Framework2 I found out that you have to use:
$data = array(1,3,4);
$select->where(array('status_id' => $data));
Result:
WHERE `status_id` IN ('1', '3', '4')
I couldn't find this documented anywhere! ZF documentation is generally sub-optimal.
apparently it is super simple... stupid me:
$select->where('status_id IN(1,3,4)');
:(
We can use Zend\Db\Sql\Predicate\In with Zend\Db\Sql\Where to make a where in query inside a model.
$this->status_ids = array(1,3,4);
// select attributes from db by where in
$result = $this->select(function (Select $select) {
$predicate = new In();
$select->where(
$predicate->setValueSet($this->status_ids)
->setIdentifier('status_id')
);
})->toArray();
$completionNo = implode(",",$data);
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$select = $db->select()->from(array("p"=>PREFIX . "property_master"),array('id','completion_no','total_carpet_area'))->where("p.completion_no IN (?)", $completionNo);
This solution works well with zf2
$ids = array('1', '2', '3', '4', '5', '6', '7', '8');
$select->where(array("app_post_id"=> $ids));
or
$ids = array('1', '2', '3', '4', '5', '6', '7', '8');
$sql = new Sql($this->adapter);
$select = $sql->select();
$select->from('app_post_comments');
$select->where(array("app_post_id"=> $ids));
// echo $select->getSqlString($this->adapter->getPlatform());
// exit;
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($result);
$resultSet->buffer()->toArray();
echo '<pre>';
print_r($resultSet);
exit;
return $resultSet;
Related
How would I modify this so that it returns a 1 row object, rather than an array?
$slot = Model_Slots::find(array(
'where' => array(
array('datetime', '=', date('Y-m-d H:i:s', $s)),
array('club', '=', $club->id),
),
));
(Solution at the moment is to follow it with... $slot = ($slot[0]) ?: false;, ugh!)
This can be done using get_one() - http://docs.fuelphp.com/packages/orm/crud.html#/find_chaining
$slot = Model_Slots::find(array(
'where' => array(
array('datetime', '=', date('Y-m-d H:i:s', $s)),
array('club', '=', $club->id),
),
))->get_one();
Both find_by_pk() and find_one_by() return a single model object, like the ORM get_one() equivalent.
The other find methods return an array of results. The easiest is to add a LIMIT 1, and use
$result and $result = reset($result);
to get the first element of the array.
I know there are some examples in the web, but doesn't work in my case.
I have a Category table with subcategories. One table 'category' that has id_father that's the id of the Parent Category.
My configuration is the following:
Application_Model_DbTable_Category:
protected $_referenceMap = array(
'Application_Model_DbTable_Category' => array(
'columns' => 'id_father',
'refColumns' => 'id',
'refTableClass' => 'Application_Model_DbTable_Category'
));
CategoryMapper (I don't write the top code, anyway I retrieve a Category that has a parent in the db)
$row = $result->current();
echo $row->name;
$father = 'Non trovato';
$father = $row->findParentRow('Application_Model_DbTable_Category');
print_r($father);
This doesn't print anything... Is something wrong on my code? Thank you
you need to create instance of class Application_Model_DbTable_Category
$row = $result->current();
echo $row->name;
$father = 'Non trovato';
$father = $row->findParentRow(new Application_Model_DbTable_Category());
print_r($father);
I would like a Zend_Form object to be output as essentially
array('name' => 'input username in db', 'description' => 'description thats in the database');
so far If I do
print_r((array) $form);
I get:
{"\u0000*\u0000_attribs":{"name":"add","enctype":"multipart\/form-data","method":"post"},"\u0000*\u0000_decorators":{"FormElements":{"decorator":"FormElements","options":null},"HtmlTag":{"decorator":"HtmlTag","options":{"tag":"dl","class":"zend_form"}},"Form":{"decorator":"Form","options":null}},"\u0000*\u0000_defaultDisplayGroupClass":"Zend_Form_DisplayGroup","\u0000*\u0000_description":null,"\u0000*\u0000_disableLoadDefaultDecorators":false,"\u0000*\u0000_displayGroupPrefixPaths":[],"\u0000*\u0000_displayGroups":[],"\u0000*\u0000_elementDecorators":null,"\u0000*\u0000_elementPrefixPaths":[],"\u0000*\u0000_elements":{"name":{"helper":"formText"},"name_url":{"helper":"formText"},"description":{"helper":"formTextarea","rows":"10"}
etcetc, alot of Zend storage
If I do:
$form = new Form_Administration_Movie_Add();
$elements = $form->getElements();
foreach($elements as $key => $element) {
echo $key;
}
I get a list of the fields, however I cannot do $element->getValue() as I just get 0 or 1 and no the actual data.
Ideas?
$array = $form->getValues();
Does this work? :)
Edit: You might need to use either $form->isValid($_POST) or $form->populate($_POST) before calling the getValues() method.
I have something like this:
$data = array('username' => 'John', 'password' => 'john123');
$table = new Zend_Db_Table('users');
$table->insert($data);
But I want it to include a hashing function to the password, so the sql is something like this:
INSERT INTO `users` (`username`, `password`) VALUES ('John', PASSWORD('john123'));
How can I do that, elegantly?
$data = array('username' => 'John', 'password' => new Zend_Db_Expr('SHA1(john123)');
I'm at my wits end in what the queries parameter value should look like. So to sumbit a multiquery accross the graph API library, the following would be the code method to perform it, as far as I can tell.
$param = array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
'callback' => '');
$queryresults = $facebook->api($param);
Using this method in the new PHP SDK library from Facebook, has any one made this work? If so can you drop an example on how you build the complete value of the $multiQuery variable?
I've been struggeling with this for a few days and I'm only finding exmaples with the old PHP library.
Why is it always after banging your head for days, you ask a question, and 5 minutes later, you come up with the answer your self.
So here was MY lovely experience.
Since in PHP you can use a "/' character to start a text string, I got my self stuck in the flip flopping of the double quote character and single quote character. It dawned on me that the queries defined in a multi query are, duh, wrapped by double quotes.
So lesson learned? If you have a where clause that uses a string value in a multi query, make sure for pete's sake you use SINGLE QUOTES around the string value your filtering on.
BAD BAD - This is what I did. note the double quotes around myvalue and myothervalue. NAUGHTY!
$multiQuery = {
"query1":"select something from something where somecolumn = "myvalue"",
"query2":"select something from something where somecolumn = "myothervalue""
};
GOOD Example - Now look at myvalue and myothervalue.
$multiQuery = {
"query1":"select something from something where somecolumn = 'myvalue'",
"query2":"select something from something where somecolumn = 'myothervalue'"
};
So now I can...
$multiQuery = {
"query1":"select something from something where somecolumn = 'myvalue'",
"query2":"select something from something where somecolumn = 'myothervalue'"
};
$param = array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
'callback' => '');
$queryresults = $facebook->api($param);
And if any of you are wondering what is the actual type of the $multiQuery variable is (for newbie like me), it's just a string data type. It's not an array, nothing more nifty than text.
Considering an array of node id's with their respective url's as values you'll have
/**
*A JSON-encoded dictionary of the queries to perform. The array contains a set of key/value pairs.
*Each key is a query name, which can contain only alphanumeric characters and optional underscores.
*Each key maps to a value containing a traditional FQL query.
*/
$fql = '{';
foreach ($path as $key1 => $value1) {
$fql .= '"' . $key1 . '":"SELECT share_count, like_count, comment_count, total_count FROM link_stat WHERE url=\'' . $value1 . '\'",';
}
$fql .= '}';
$param = array(
'method' => 'fql.multiquery',
'queries' => $fql,
'callback' => ''
);
try {
$fqlresult = $facebook->api($param);
} catch (FacebookApiException $e) {
watchdog('Facebook Query', 'Parsing error on node #node | #error', array('#node' => $key1, '#error' => $e), WATCHDOG_DEBUG); }
You can try this:
$multiQuery= array ("query1" => "query #1 goes here","query2" => "query #2 goes here");
$param = array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
'callback' => '');
$queryresults = $facebook->api($param);