English 中文(简体)
避免重复 数据库记录
原标题:Avoiding duplicates Database records in a datamapper code igniter

My Question is simply how to insert data into the join table(user_book) without inserting a duplicate entry into the main table(book).

主要考虑我们的树木表如下:

 id  Users id Books  id Users Books 
  1   Sam   1   B1     1   1    1
  2   Ben   2   B2     2   1    3  
  3   Mike  3   B3     3   3    3
                       4   2    2

但是,在我插入一本新书(如Mike等人,B3)时,问题就在于此。 复制件将出现在账簿表上,上表如下:

  id  Users id Books  id Users Books 
  1   Sam   1   B1     1   1    1
  2   Ben   2   B2     2   1    3  
  3   Mike  3   B3     3   3    4
            4   B3     4   2    2

现在,这是否使我试图解决的是什么意思? 或者,我根本不可能有一个独特的书籍清单?

if I am going to insert Given the database users and books users<--->books are related by users_books we need to make sure that when we insert a book record to the db it is not a duplicate. Further, if the book exist, a relation will be inserted and not the book record . How can one do that? One approach is to say

$m=new Book();  
$m->where( id_book , $data[ user_booklist ][$i][ id ])->get();
$u = new User();
$u -> where( id_user , $data[ user_profile ][ id ])-> get();
if(!$u->where_related($m))
{
$m -> name = $data[ user_booklist ][$i][ name ];
$m -> id_book = $data[ user_booklist ][$i][ id ];                                               
$m -> save($u);  
}

if (book->exist) in the "books" table then check the relation and see if there is a relationship between users and books and if there is then don t insert it. Further do I need to change any thing in the mysql db structure to avoid this.

The above code is not working but should give an idea what I am talking about.

最新情况:

In summary, if two users liked the same book, I just want to insert a record(book) into the join table (users_books) and while not to creating a new record in the table. Using the unique sql tag did not work since it keeps unique list but it prevents from inserting the multiple relationships into the join table users_books.

最佳回答

你可以做到:

$b = new book();  
$b->where( id_book , $data[ user_booklist ][$i][ id ])->get();
// I believe you can do this as well, but I m not sure:
// $b->where( id , $data[ user_booklist ][$i][ id ])->get();

$u = new user();
$u -> where( id_user , $data[ user_profile ][ id ])-> get();

if( $b->exists() && $u->exists() )
{
   // This saves the relationship between the book and the user
   $b->save($u);
}
else  
{
   // Could not find the book or the user
}

You should check out the datamapper-manual on saving relationships.

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签