Part of the EllisLab Network
   
22 of 25
22
Gas ORM
Posted: 07 February 2012 08:22 PM   [ Ignore ]   [ # 211 ]  
Summer Student
Total Posts:  4
Joined  06-25-2007
toopay - 07 February 2012 07:16 PM

@vinhluu,

You could use something like :

$post Gas::factory('post')->select("COUNT(*) AS stat")
                            ->
like('title''something')
                            ->
limit(1)
                            ->
all();

echo 
'Total post(s) contain "something" :'.$post->stat

But frankly, the gas model and therefore, its API, was design to work with object and entity, so i think it will appropriate to have some method to handle those statistic task in corresponding model, rathen than having above cumbersome syntax everywhere in your script just to count the result. I will probably having something like this in ‘post’ model for example :

public function count_like($collumn$value)
{
  
// In your gas model scope
  // $this->db() was analog of $this->db (in native CI model)
  
return $this->db()->like($collumn$value)
                    ->
from($this->table)
                    ->
count_all_results();
}
// Then just call it with :
// Gas::factory('post')->count_like('title', 'something'); 

It would be nice to have a finder function ‘count_where’ as a part of the core. This ‘count_where’ would mainly be used in obtaining pagination recordset.

Example:

$sqlCondition" title like '%" $this->input->get("keyword") . "%'";
  
  
// get match count
  
$resultCount Gas::factory('post')->count_where($sqlCondition);
  
  if(
$resultCount 0)
  
{
   
// get paged result set
   
$currentPage = ($this->input->get('page') <> "" $this->input->get('page'): );
   
   
$config['total_rows'$resultCount;
   
$this->pagination->initialize($config); 
   
$paging $this->pagination->create_links() ;   
   
   
$limit 20;
   
$offset $currentPage $limit;
   
$resultSet Gas::factory('post')->find_where($sqlCondition$limit$offset);
  

 

Profile
 
 
Posted: 07 February 2012 09:58 PM   [ Ignore ]   [ # 212 ]  
Summer Student
Total Posts:  19
Joined  07-27-2011

Why does this library create a second database entry in the output profiler?

When I do $this->output->enable_profiler() I get:

DATABASE:  test   QUERIES: 1 (Hide)
0.0005   SELECT *
FROM (`tbl_user`)

DATABASE:  test   QUERIES: 1 (Hide)
0.0005   SELECT *
FROM (`tbl_user`)

Profile
 
 
Posted: 07 February 2012 11:28 PM   [ Ignore ]   [ # 213 ]  
Summer Student
Total Posts:  19
Joined  07-27-2011

Another question: Why do the objects not get updated after a save()?

For example,

$user Gas::factory('user')->find(1);
echo 
$user->username// "testusername"

$user->username "updateusername";
$result $user->save();

echo 
$user->username// still "testusername" instead of "updateusername", even though it did update the database 

 

Profile
 
 
Posted: 08 February 2012 06:28 AM   [ Ignore ]   [ # 214 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1544
Joined  12-20-2010
vinhluu - 07 February 2012 08:22 PM

It would be nice to have a finder function ‘count_where’ as a part of the core. This ‘count_where’ would mainly be used in obtaining pagination recordset.

Gas ORM has extension that design to resolve other task(s). Personaly i think, something like jquery extension works well for interactive table, including its pagination…what you think?

If something like that, doesnt enough for you, you always could write your own extension covering pagination stuff.

jellysandwich - 07 February 2012 09:58 PM

Why does this library create a second database entry in the output profiler?

Dont autoload database. Gas already load it automatically. I already put notification at the bottom of this page

jellysandwich - 07 February 2012 11:28 PM

Another question: Why do the objects not get updated after a save()?

Any write operations(INSERT, UPDATE, DELETE), uniformly return boolean, not an object. Thus, the previous fetched record, still there. That would be handy for chained procedure (eg, after update some record, you want modify any related entries too, you could still use the previous PK for that).

This mean, you will need to load the fresh one, eg :

$user1 Gas::factory('user')->find(1);
$user1->name 'New name';
if (
$user1->save)
{
  
echo 'User1 name become:'.Gas::factory('user')->find(1)->name;

 

 Signature 

“In Code We Trust.”


CI Library : Gas ORM | Proxy

Profile
 
 
Posted: 12 February 2012 12:51 PM   [ Ignore ]   [ # 215 ]  
Summer Student
Total Posts:  23
Joined  12-08-2007

Hi Toopay,
I’m getting some weird behaviour in record insertion…  I have a permissions table with no relations or timestamp fields defined as follows:

public $table 'permissions';
public 
$primary_key 'id';

function 
_init()
{
 $this
->_fields = array(
  
'id' => Gas::field('auto[11]', array(), ''),
  
'parent_id' => Gas::field('int[11]', array(), ''),
  
'permission_key' => Gas::field('char[32]', array(), '')
 );

I create a new Permissions object and fill the variables:

$perm = new Permissions;
$perm->permission_key "SCEN-1";
$perm->parent_id 14;
var_dump($perm);
$perm->save(); 

The vardump gives this output

object(Permissions)#57 (17) { 
 
["table"]=> string(11"permissions" 
 
["primary_key"]=> string(2"id" 
 
["relations"]=> array(0{ } 
 [
"empty"]=> bool(true
 
["errors"]=> array(0{ } 
 [
"locked"]=> bool(false
 
["single"]=> bool(false
 
["extensions"]=> array(0{ } 
 [
"_fields":protected]=> array(0{ } 
 [
"_unique_fields":protected]=> array(0{ } 
 [
"_ts_fields":protected]=> array(0{ } 
 [
"_unix_ts_fields":protected]=> array(0{ } 
 [
"_set_fields":protected]=> array(2
 [
"permission_key"]=> string(6"SCEN-1" 
 
["parent_id"]=> string(2"14" 
 [
"_get_fields":protected]=> array(0{ } 
 [
"_get_child_fields":protected]=> array(0{ } 
 [
"_get_child_nodes":protected]=> array(0{ } 
 [
"_get_reflection_fields":protected]=> array(0{ } 


And the call to save() generates this error

Unknown column 'created_at' in 'field list'
INSERT INTO `permissions` (`permission_key`, `parent_id`, `created_at`) 
 
VALUES ('SCEN-1''14'1329067679

The function being used to generate permissions has worked several times prior to this call so I know it works…  the previous times, the field “created_at” was not added. Any thoughts?

Thanks again for your help!

Profile
 
 
Posted: 13 February 2012 06:03 AM   [ Ignore ]   [ # 216 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1544
Joined  12-20-2010

@mecharius, i cant reproduce those weirdness.

What happen if you change

$perm = new Permissions

above into

$perm Gas::factory('permissions'); 

If the weirdness still occur, i will need to see more chunk of code you use, around those saving process, to do some debugging trace.

 

 Signature 

“In Code We Trust.”


CI Library : Gas ORM | Proxy

Profile
 
 
Posted: 15 February 2012 12:12 PM   [ Ignore ]   [ # 217 ]  
Summer Student
Total Posts:  18
Joined  12-11-2008

Can i somehow create references to other tables (foreign key) depending on the actual object i’m using? It’s easier with example.

I have “reservables”, things that can be reserved for something. Reservables have type-field, that defines the object that has more info about reservable on 1:1 relationship.

Reservables
array[0] =
{
id = 1,
type = ‘room’,
..other info..
}
array[1] =
{
id = 2,
type = ‘field’,
..other info..
}

That means, that i’ve general table “reservables” that has general information. On top of that, i’ve tables ‘room’ and ‘field’ that have more information of rooms or fields. So for reservable id 1 i’ve entry in Room table with foreign key 1, that has information of availibility, beds, so on. And same way, reservable id 2 has more info about it in table Field, that may have god know what information.

Now Gas Orm should somehow be able to know, that some records have $record->room->info and some have $record->field->info

Or is it a problem at all? I’m just about to start using Gas and i’m wondering about the thing above. Or do i break some db design rules if i’ll do that?

Profile
 
 
Posted: 15 February 2012 01:28 PM   [ Ignore ]   [ # 218 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1544
Joined  12-20-2010

@Jazmo,
If i correctly get what you are trying to do, this schema will allow you to do it better :

+---------------+   +--------------+   +------------+
reserve       |   | reserve_type |   | room       |
+---------------+   +--------------+   +------------+
id            |<->| reserve_id   |   |            |
other_field   |   | room_id      |<->| id         |
| ...           |   | 
other_id     |   | info       |
+---------------+   +--------------+   | .........  |
                                       +------------+ 

reserve_type act as pivot table that could bridge reserve to room and other table(s). other_id could reference to other table as well, and you could set up several related entities with different type of relationship(s), within reserve model using through option.

 Signature 

“In Code We Trust.”


CI Library : Gas ORM | Proxy

Profile
 
 
Posted: 16 February 2012 09:56 AM   [ Ignore ]   [ # 219 ]  
Summer Student
Total Posts:  18
Joined  12-11-2008

Good idea toopay! Thanks for your help.

Profile
 
 
Posted: 17 February 2012 07:05 PM   [ Ignore ]   [ # 220 ]  
Summer Student
Total Posts:  23
Joined  12-08-2007
toopay - 13 February 2012 06:03 AM

@mecharius, i cant reproduce those weirdness.

What happen if you change

$perm = new Permissions

above into

$perm Gas::factory('permissions'); 

If the weirdness still occur, i will need to see more chunk of code you use, around those saving process, to do some debugging trace.

Hi Toopay, sorry for the late reply… very busy week.  I just changed the code to use

Gas::factory('xxxxx'); 

but it still does the same thing.  Very weird - it only does it creating permissions for one particular model.  This happens even if I don’t pass the permissions function that model - i.e. just an integer ID to grant permission to.

My current work around is just to add a “created_at” column in the database which is mostly nulls. I’ll post back here if I work it out.

Thanks! Will

Profile
 
 
   
22 of 25
22