Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]Damir Sivic[/eluser]
Let us see the code for deletion...

[eluser]kakallatt[/eluser]
[quote author="Damir Sivic" date="1367950191"]Let us see the code for deletion...[/quote]

Code:
$user = new User();

$user->get_by_id($id);

// Show error if user not exists
$user->exists() || show_error('Invalid user id.');

// Stored username and deleting
$username = $user->username;
$user->delete();

[eluser]Damir Sivic[/eluser]
your relationship is one on one,

your code:
Code:
class User extends DataMapper {

var $has_one = array( 'page');
}


class Page extends DataMapper {

var $has_one = array( 'user');
}

and it should be many to one, or many to many

Code:
class User extends DataMapper {

var $has_many = array( 'page');
}


class Page extends DataMapper {

var $has_one = array( 'user');
}

[eluser]Damir Sivic[/eluser]
I have tables:
- schools
- users

in table users are teachers and students, I want to separate association for teachers in one table "schools_teachers" and students in the another table "schools_students",
is this posibble with DM.

[eluser]Damir Sivic[/eluser]
got it...
Code:
class School extends DataMapper
{
// Insert related models that Template can have more than one of.
public $has_many = array(
  'teacher' => array(
   'class' => 'user',
   'other_field' => 'school2',
   'join_table' => 'schools_teachers',
   'join_other_as' => 'teacher',
   'model_path' => 'application/modules/users'),
  'student' => array(
   'class' => 'user',
   'other_field' => 'school',
   'join_table' => 'schools_students',
   'join_other_as' => 'student',
   'model_path' => 'application/modules/users')
  );
}

class User extends DataMapper
{
// Insert related models that Template can have just one of.
//public $has_one = array("group");

// Insert related models that Template can have more than one of.
public $has_many = array(
  
  'school2' => array(
   'class' => 'school',
   'other_field' => 'teacher',
   'join_table' => 'schools_teachers',
   'join_other_as' => 'school2',
   'model_path' => 'application/modules/schools'),
  'school' => array(
   'class' => 'school',
   'other_field' => 'student',
   'join_table' => 'schools_students',
   'join_other_as' => 'school',
   'model_path' => 'application/modules/schools')
  );
}


[eluser]nickaceph[/eluser]
Hi, I have decided from a lot of research to use CI and Datamapper for my Project its a kind of framework application layer build on top of CI and Datamapper which is very much fit for my needs,

But with a missing piece of functionality,
Going back and forth with Ci and DB changes etc. is just as hard when you need to focus on your code.
Is DataMapper ORM can generate tables from Model? is there any extension available? so I would not have to create it myself.

Thanks

Nick Ace

[eluser]kakallatt[/eluser]
Code:
$count = $article->where_related_category('id', $category->id)->count();

But I got the message:
Quote:Unknown column 'dk_articles_categories.category_id' in 'where clause'

SELECT `dk_articles`.*, `dk_users`.`username` AS user_username FROM (`dk_articles`) LEFT OUTER JOIN `dk_users` dk_users ON `dk_users`.`id` = `dk_articles`.`user_id` WHERE `dk_articles_categories`.`category_id` = 8 ORDER BY `dk_articles`.`id` desc LIMIT 3

[eluser]WanWizard[/eluser]
This query is not produced by that like of code (alone)?

Is there code earlier in your method which defines a different query on $article that was never executed? This line doesn't add a limit of 3, and doesn't add a User object and a select of the users username...

[eluser]kakallatt[/eluser]
I don't really understand your reply because my english is bad, so:
There is my method:
Code:
public function category($slug) {
  // Create objects
  $obj_category = new Category();
  $obj_article = new Article();

  $obj_category->get_by_slug($slug);

  // Show 404 if not found
  $obj_category->exists() || show_404();

  // Load stuff
  $this->load->helper(array('post', 'text'));
  $this->load->library('pagination');

  // Get page number
        $page_number = ($this->uri->segment(4)) ? $this->uri->segment(4) : 1;

        // Count all of rows
        $count = $obj_article->where_related_category('id', $obj_category->id)->count();

  // Config pagination
        $config['base_url']             = site_url('blog/category/' . $slug);
        $config['uri_segment']          = 4;
        $config['total_rows']           = $count;
        $config['per_page']             = 3;
        $config['use_page_numbers']     = TRUE;

        // Show error if page number is not a number
        is_numeric($page_number) || show_error('Invalid page number.');
        $offset = ((int) $page_number == 1) ? 0 : ((int) $page_number - 1) * $config['per_page'];

        // Initialize pagination
        $this->pagination->initialize($config);
                                
        // Fetch all articles in the category
  $articles = $obj_article->where_related_category('id', $obj_category->id)
        ->include_related('user', 'username')
        ->get_iterated($config['per_page'], $offset);

  $this->template->title('Category : ' . e($obj_category->name), $this->settings->get('site_name'))
        ->set('articles', $articles)
        ->build('category');
}

Occurs error in this line:
Code:
$count = $obj_article->where_related_category('id', $obj_category->id)->count();

[eluser]WanWizard[/eluser]
All I was saying that the query you showed in your previous post is not the query generated by the line you say produces the error.

Given the fact that that SQL was a SELECT which included the user data, and had a LIMIT, I would say that it is produced by the get_iterated() query.

First thing to do is to make sure you have the latest version, get it from bitbucket if needed. If the problem persists, you can create a ticket for it.

I have to say that I haven't done anything with CI in over two years, which makes allocating time to debugging and testing difficult.




Theme © iAndrew 2016 - Forum software by © MyBB