Returning 1 row using Model_Crud in FuelPHP - fuelphp

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.

Related

How to select a calculated value including parameter using Zend FW?

I'm using Zend FW and have a select with specified columns to be selected, e.g.
$select = $this->_db->select();
$select->from( array("t" => 'table'), array( 'id', 'name', 'some_number'));
Now I want to get the some_number value multiplied by some given parameter, like :
select id, name, (some_number * $param) from table;
How to do that?
Got it, using Zend_Db_Expr. If someone else needs... That works for me:
$select->from( array("t" => 'table'),
array( 'id', 'name', 'new_number' => new Zend_Db_Expr('some_number * ' . $param ) )
);

How to return results from a mysqli query within a class and format them outside the class

I'm gettings started using mysqli prepared statements and am trying to write a class method that retrieves records matching certain date criteria, but does not display them -- i want to be able to format the result outside of the class.
i have the class method working when it displays the results:
public function periodReceipts(){
global $db;
if($query = $db->prepare("SELECT * FROM receipts WHERE date BETWEEN ? AND ? ORDER BY date ASC")){
$query->bind_param("ss", $this->date1, $this->date2);
$query->execute();
$query->bind_result($id, $user_id, $vendor, $amount, $cat, $date);
$query->fetch();
while($query->fetch()){
echo "$id, $user_id, $vendor, $amount, $cat, $date <br/>";
}
$query->close();
}
}
and i have a similar method working that performs a mysqli query, retrieves records, calculates a total, and returns that result, unformatted, to display later:
public function runningTotal(){
global $db;
if($query = $db->prepare("SELECT amount FROM receipts WHERE user_id = ?")){
$query->bind_param("i", $this->uid);
$query->execute();
$query->bind_result($amount);
$running_total = 0;
while($query->fetch()){
$running_total += $amount;
}
$query->close();
}
return $running_total;
$db->close();
}
but i can't figure out how to get the method periodReceipts() to behave similarly. I assume i need to get the data into an array, but how do i do that, and how do i access it later?
thanks!
Change your method like following
...
$result = array();
while($query->fetch()){
$result[] = array(
'id' => $id,
'user_id' => $user_id,
'vendor' => $vendor,
'amount' => $amount,
'cat' => $cat,
'date' => $date
);
}
$query->close();
return $result;
...
Then you'll get all your records in array:
$result = $your_class_instance->periodReceipts();
var_dump($result);

Passing an array as a value to Zend_Filter

I'm trying to implode an array by using the Zend_Filter_Interface.
Here's my simplified test case.
class My_Filter_Implode implements Zend_Filter_Interface
{
public function filter($value)
{
return implode(',', $value);
}
}
The input will be an array.
$rawInput = array('items' => array('a', 'b', 'c'));
$validators = array(
'items' => array()
)
$filters = array(
'items' => 'Implode'
);
$filterInput = new Zend_Filter_Input($filters, $validators, $rawInput, $options);
I would like the filter to transform array('a', 'b', 'c') to a string 'a,b,c'. It is instead applying the filter to each item in the array. How can the value passed to filter() be passed as an array?
Your filter seems to be OK. The problem is in Zend_Filter_Input, which passes individually the values in the array to the filters and validators.
There is a discussion thread about this problem, along with some possible workarounds here: http://zend-framework-community.634137.n4.nabble.com/Zend-Filter-Input-and-Arrays-td653511.html
Hope that helps,

Output Zend Form To Array of Key => Value

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.

Facebook New PHP SDK For Graph API - Multi Query

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);