Welcome Guest, Not a member yet? Register   Sign In
Gas ORM
#11

[eluser]toopay[/eluser]
@brunobarros,

In version 2.0, there will be more flexible way to do that. In version 1.X, i'm affraid, it was the only solution. Not too pretty for sure, to have those, but at least it works as expected Smile
#12

[eluser]quasiperfect[/eluser]
i tried to use gas but i get a error maybe someone can help
the error is Fatal error: Class 'User' not found

i use latest gas,codeigniter from git
and @wiredesignz hmvc extension

i even tried to load user model but no luck i get Fatal error: Call to undefined method Gas::load_model()

in my gas config i have
Code:
$config['models_path'] = array(APPPATH.'models', APPPATH.'modules');
$config['models_suffix'] = '_gas';
$config['autoload_models'] = TRUE;
$config['cache_request'] = TRUE;
$config['auto_create_models'] = FALSE;
$config['auto_create_tables'] = TRUE;

i have models/user_gas.php
Code:
class User extends Gas
{
public $table = 'users';
public $primary_key = 'id';
public $relations = array(
  'has_many' => array('comment' => array()),
);

function _init()
{
  $this->_fields = array(
   'id' => Gas::field('auto[11]'),
   'username' => Gas::field('char[6,15]'),
   'password' => Gas::field('char[6,15]'),
   'email'    => Gas::field('email'),
   'created_at' => Gas::field('datetime'),
   'updated_at' => Gas::field('datetime'),
   'deleted_at' => Gas::field('datetime'),
   'delted' => Gas::field('int[1]'),
  );
  $this->_unique_fields = array('email', 'username');
  $this->_ts_fields = array('updated_at');
}
}

and i call gas like this
Code:
$data = array(
   'username' => 'myusername',
   'email'    => '[email protected]',
   'password' => md5('[email protected]'),
   'created_at' => date("Y-m-d H:i:s"),
   'updated_at' => date("Y-m-d H:i:s"),
   'deleted_at' => '',
   'delted' => 0,
  );
  $new_user = new User;
  $new_user->fill($data, TRUE);
  if ( ! $new_user->save(TRUE))
  {
         echo $new_user->errors('<p class="error">', '</p>');
         echo 'The raw errors were : ';
         print_r($new_user->errors);
  }
  else
  {
         echo 'New user successfully created. And her id is '.$new_user->last_id();
  }
#13

[eluser]toopay[/eluser]
I cant replicate those behaviour. What version you are using? In very earlier version, it just accept string at `models_path` configuration.
#14

[eluser]khavayero[/eluser]
Hello Toopay,

I have a easy questions,

When I use static method for save a record, the method errors is empty, why?

Example:

Code:
$sales = new Sales();
if ($sales->fill($arrSale, TRUE)->save(TRUE)){
   echo "ok";
}else{
   print_r($sales->errors); //out: [name_field] : .....error description
}

<b>But.....with static method</b>

Code:
if (Gas::factory('sales')->fill($arrSale, TRUE)->save(TRUE)){
   echo "ok";
}else{
   print_r(Gas::factory('sales')->errors); //out: is empty
}

Thanks a lot!!

#15

[eluser]quasiperfect[/eluser]
@Toopay i solved my previous problem by redownloading gas

now i have a diferent question

let's take as example a dating site where a user has a profile page and people can post comments on that page and comments to comments

each comment is created by a user for another user and belongs to some user, or some comment

my tables are
Code:
CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(15) NOT NULL,
  `password` varchar(48) NOT NULL,
  `email` varchar(50) NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `deleted_at` datetime DEFAULT NULL,
  `deleted` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `comments` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(10) unsigned DEFAULT NULL,
  `from_user` int(10) unsigned NOT NULL,
  `to_user` int(10) unsigned NOT NULL,
  `text` text NOT NULL,
  `updated_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `deleted_at` datetime DEFAULT NULL,
  `deleted` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`),
  KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
i don't know if i make any sense
if you don't understand tell me and i will try to make a better example

how can i define the models for this situation ?
how can i get all the comments for a user ?
if i delete a comment that has children will the children be deleted ?

best regards,
ionut
#16

[eluser]toopay[/eluser]
@khavayero,
There are at least two same reports, regarding those issue. But at this momment, i really dont have a time to do throughout test, related with that issue (and auto-time stamp issue in some place), due i am in a serious attemp to make most of PDO driver(s) fully working on CI, since latest version of Gas will rely (or at least i will recomend anyone to do so) on PDO. So, for now, you could use that factory method to hydrate a Gas instance instead instantiate it.

@quasiperfect,
At least, i could see a potential issue, with those schema. I probably will ended to this schema, for those task :
Code:
# Note that each of gas model, could have more than one relationship type as long as different relationship type not pointing into same referal table.

  +--------+   +--------------+   +------------+
  | user   |   | comment_user |   | comment    |
  +--------+   +--------------+   +------------+
+>| id     |<->| user_id      |   |            |
| | name   |   | comment_id   |<->| id         |<-+
| | ...    |   |              |   | parent_id  |<-+
| +--------+   +--------------+   | .........  |
|                                 | sender_id  |<-+
|                                 +------------+  |
+-------------------------------------------------+
Read through documentation regarding how to set up those each relationship type.
#17

[eluser]quasiperfect[/eluser]
@toopay thanks for responding

not sure if i get your schema
i will need 3 models ?
or i need to define multiple relationships in user and comment ?

#18

[eluser]Jazmo[/eluser]
What is the status of 2.0? I'm about to start to use Gas to my projects, should i start with 1.4 or go directly for 2.0?
#19

[eluser]quasiperfect[/eluser]
i think i got the hang of it

i have some problems with saving

how i can save related objects

i have users with a table comments with another
the relationship is has_and_belongs_to with a join table that should contain the id's of user and related comment

we get the user by $new_user = Gas::factory('user')->find(1);

how can i add a new comment to the user ?

comment addtionaly to the relation with the user is related to self

how can i save a new comment to the same user but as a reply to the previos comment ?

thanks in advance for any help

#20

[eluser]toopay[/eluser]
@quasiperfect,

So in your case, you need to save an entry within 'comment_user' to related a newly created comment to some user, yes? You could perform additional task within a model in save state, either at before_save or after_save hooks. You will either have to write a raw SQL to insert that related entry or use CI query builder analog method. eg :
Code:
function _after_save()
{
  if ($this->empty)
  {
     // This will triggered, if you INSERT new comment
     //....
     // Assume you already wrap needed entries into variables
     $this->query("INSERT INTO comment_user (comment_id, user_id) VALUES ($comment_id, $user_id)");
     // or if you comfort with the way CI query builder, you could use $this->db() as analog of $this->db
     // $this->db()->insert('comment_user', array('comment_id' => $comment_id, 'user_id' => $user_id));
  }
  else
  {
     // This will triggered, if you UPDATE some comment
  }
}

This post asking the same thing, and i answer why gas doesnt need those.

@Jazmo,
My application(s) still using version 1.X. Its stable, despite there is small issue on object instantiation which easily resolved by using factory method. Iam not going drop support for this version, and those issue will be fixed in near time. But there will be no additional feature develop on it, in the future. While this version fits with most of typical CI project, if by any reason, your project become grow and you think you need all PHP5 benefits to help you in your gas model(s), you could upgrade later. I will make sure upgrading process from this older version to new version, as easy as possible.

Version 2 is almost complete, but still not recommended to anyone to use it on any actual project in this time. It tend for one who think they deserve to use PHP5 benefits all over the place. It use namespace, spl autoloader, and many magic method(s), which would need PHP 5.3 as requirement. And iam also in an attemp to make the latest CI fully support for PDO, since this new version will rely on PDO as the main DBAL (however, will still alow native driver(s) as secondary option). You could track the status of this version on the Gas ORM repo.




Theme © iAndrew 2016 - Forum software by © MyBB