Welcome Guest, Not a member yet? Register   Sign In
IgnitedRecord 1.0 pre-release

[eluser]kostia[/eluser]
m4rw3r,

the rule didn't work killing the checked string.

in terms of speed and simplicity the code below works better.

Please correct the code to pass error without changing your IR_validatioin_lang file.

Code:
class test extends IgnitedRecord{
   var $act_as = array(
            'timestamped',
            'validation' =>
            array('test' => array('rules' => 'required|max_length[32]|textstring'));
  
   function textstring($str)
    {
        if (strspn($str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 +-_,.:;'!?#$^&") != strlen($str))
        {
            pass error here! = "The %s field can contain only alphanumerical characters, space and punktuations";
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}

[eluser]m4rw3r[/eluser]
I've made the add_error() a static method in the IR_validation class, so you call IR_validation::add_error('Your error string here'); to set the error.

The string is passed to sprintf(), with the field name as first parameter and the rule parameter as the second.

[eluser]porky207[/eluser]
Hello everybody, I'm looking for a way to sneak a CASE order-by statement into a query generated by IR.

Say I have an e-shop and a customer looking for a brush. He enters "brush" into my mask field, but only gets "Brushless motors" suggested in hints, because aside from brushes I carry many motors and apply LIMIT to the query. The best match, "Brush", could that way easily be left unreturned.

My code:
Code:
$arrProd = $this->Product->
        ->where()
            ->like('products.name', $mask) # like '%mask%'
        ->end()
        ->limit(10,0)
        ->find_all();

Hardcoding the query is one way to do it, but would anyone here have an idea how to gracefully work a case-based, bound ORDER BY like this into the query, so that it makes maximum use of what's already written?

Code:
ORDER BY (
         CASE
         WHEN products.name    =    ?    then    1
         ELSE    0.1
         END
     ) DESC

Thank you!

[eluser]m4rw3r[/eluser]
I've now added the possibility to add the ORDER BY criteria without escaping (latest svn).
Set the $direction (second parameter) to false, and the $by part will be appended to the ORDER BY part (if you use multiple, they will be separated by commas in the order called).

Example:
Code:
->order_by('(
         CASE
         WHEN products.name = '.$something.' then 1
         ELSE 0.1
         END
     ) DESC', false);

btw, in your case you don't need the nested where (->where()->*->end()).

[eluser]porky207[/eluser]
Ok, now I can get the CASE in--thank you for modifying the direction behavior! /p

[eluser]phme[/eluser]
Hi all,

Just trying IR for the first time today, & I have a lame question. I'm not sure it's not somewhere in the manual, but I couldn't find it. I apologize if it's really obvious.

I have:
Code:
// model:
class Author extends IgnitedRecord
{
  var $table = 'authors';
  var $has_many = array('post');
}
class Post extends IgnitedRecord
{
  var $table = 'posts';
  var $has_one = array('author');
}


// controller:
function index()
{
    $data['authors'] = $this->author->find_all();
    $this->load->view('author/index', $data);
}

// in the view:
<?php foreach $authors as $author:?>
  Name: <?=$author->name?>
<?php endforeach;?>

In the controller (or in the model), how can I load, for each author, all the related posts, so that I can do in the view something like:
Code:
<?php foreach $authors as author?>
  Name: &lt;?=$author->name?&gt;<br>
  &lt;?php foreach $author->posts as $post?&gt;
    Post title: &lt;?=$post->title?&gt;
  &lt;?php endforeach;>
&lt;?php endforeach;>

I understand how I can load all related records for one specific record, but I don't get how I can load all related records for each record at once.

Thanks!

[eluser]m4rw3r[/eluser]
If you want to load all related records at once, you should use the join_related() method (only works in 1.0).

Example:
Code:
// Also added some custom ordering of the results below
$data['authors'] = $this->author->join_related('posts')->order_by('authors.name, posts.title', 'desc')->find_all();
You can use your view file without modifications.

btw, IR usually figures out the tablename by itself (modelnname + s) so it is not necessary to set it.
Also, you've named the Author has many Post wrong, it should be plural (Author has many posts)

[eluser]kostia[/eluser]
m4rw3r,

I want to pass an IR object to a function. Then I want to extract fields and data as an array. It is required to process in array functions like array_intersect_key. Supposedly The number and names of the fields are not known in advance:
Code:
// model humans

function check_existence($entry = array())
{
$interestedInTheFollowingFields = array( 'name' => 'yes', 'age' =>'yes');
$entryselection = array_intersect_key($entry, $interestedInTheFollowingFields);
//do process data similarity analysis for the selected fields
//return $id for similar record or FALSE
}

Code:
//controller

$entry = $this->humans->new_record();
$entry->validate($fields);
if (!$this->humans->check_existence($entry)) // check if a similar human already in the list
{
//create new record
}
else
{
//modify existing record, which has a striking similarity
//defined by the $interestedInTheFollowingFields
}
How to create an $entryarray=array() out of the data fields contained in $entry=(object)?

thanks!

[eluser]m4rw3r[/eluser]
Use the get_data() method of the record object, it will return an associative array with the column name as key and data as value.

Note: get_data() will only return the data which has property names matching the column names in the linked table.

[eluser]dcunited08[/eluser]
I need to be able to do a 'find_all' without selecting all the columns (table.*). As far as I can tell, there is no simple way to do such a thing in a model extending ignitedrecord. The native-like 'get' (in ignitedquery) is also inaccessible due to method overriding. Basically I am doing an aggregate statement like so:
Quote:$this->select(array('ThicknessNameOut', 'WidthNameOut', 'LengthNameIn'));
$this->select_sum('BoardFeetIn');
$this->select_sum('SumPieceCount');
$this->from($this->table);
$this->where('BoardFeetIn > ', 1);
return $this->find_all();

Any ideas?




Theme © iAndrew 2016 - Forum software by © MyBB