CodeIgniter Forums
Is_Unique validation with custom db table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Is_Unique validation with custom db table (/thread-50681.html)



Is_Unique validation with custom db table - El Forum - 04-04-2012

[eluser]wehappyfew[/eluser]
Hello to all,

i am trying to create a custom table for every user that is creating an account in my site.
For example:

Code:
function add_client($form_inputs)
{
  //create the table name
  $username = $this->session->userdata('username');
  $table_name = $username . "_clients";
  //
  $this->db->insert($table_name,$form_inputs);
}
The above works fine.
BUT...i would like something more.
I would like a form validation to check for unique values in the custom table.

for example:

Code:
$field_rules =array
               (
                     'field'   => 'client_name',
                     'label'   => '-Client\'s name-',
                     'rules'   =>
required|
max_length[30]|
----------------> is_unique[{$username}_clients.client_name]|
alpha_dash|trim'
               )
The thing is that i dont know how to "integrate" the username variable into the validation.
Thank you in advance!


Oh..i though of something else that may be a solution.
It would be the same if i could create a table from the DBFORGE class ( create_table() ) where i could set the colunm index as UNIQUE.
But it seems that is not possible to set the column index anyway.


Is_Unique validation with custom db table - El Forum - 04-04-2012

[eluser]CroNiX[/eluser]
Why wouldn't you use a normalized database? Having a separate table for each user doesn't sound very efficient at all, unless each one will have unique fields that the others don't. What happens if you need to get data on all users?


Is_Unique validation with custom db table - El Forum - 04-04-2012

[eluser]wehappyfew[/eluser]
Hi,
actually this is my first big scale application.
I am not a database expert but it seems as really simlified the way i think of it.

I want every user that creates an account to have his own client's table.
And every client to have his own 2 tables: invoices and suppliers .

With the proper prefixes it should be a breeze to manage them....i think.
I would really prefer to be able to manage the database through a quick glance and not with complex queries.
And surely i dont want to mix the different users data in any way.


Is_Unique validation with custom db table - El Forum - 04-04-2012

[eluser]CroNiX[/eluser]
You should really look into normalized databases and what it means. The way you are going about this will only cause problems and a LOT more work in the end.

You should be using a single table for:
-users
-invoices
-suppliers

The invoices and suppliers tables would also have an index of the user id they belong to. Data wouldn't get mixed. You can have many invoices belonging to a single user and the same with suppliers. This is the proper way to work with relational databases and there is tremendous advantage to doing it this way. I'm sure anyone who has been using databases for a long time, or who has had any database classes, will agree.


Is_Unique validation with custom db table - El Forum - 04-06-2012

[eluser]wehappyfew[/eluser]
Hi
i've been educating myself on normalization and i must admit that it is right for my case.
Although my application is not going to be the next twitter, there would be just too many tables!

Though ,I have some questions...
1. should i have a separate id column for every table, like client_id,supplier_id or can I ,in some cases, use as an id ..lets say a column that has unique values like country_code -> DE.
in the countries table isn't it redundant to have both country_id and country_code?

2.the CRUD queries are going to be simpler that way? Or should i learn JOINS really well? I would prefer to be able to enjoy the benefits of CI's ActiveRecord class and harvest the flexibility of its queries.




Is_Unique validation with custom db table - El Forum - 04-06-2012

[eluser]xjermx[/eluser]
I am also trying to get unique validation to work.

I am working with CodeIgniter/Doctrine.

In my case, I have a registration form, and I want to check to be sure that any email address entered is unique.

I have tried this:
Code:
$this->form_validation->set_rules('email', 'E-mail',
                 'required|valid_email|is_unique[users.email]');

Which returns:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Signup_form::$db

Filename: libraries/Form_validation.php

Line Number: 954


Fatal error: Call to a member function limit() on a non-object in /../systemFolder/libraries/Form_validation.php on line 954

I have also tried following the example on the form_validation page in the user guide for CI:

Code:
$this->form_validation->set_rules('email', 'E-mail', 'callback_email_check');
(along with an email_check function, basically copy/pasted from the example in the user guide)

This gives me:

Quote:Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'email_index'' in /../applicationFolder/libraries/Doctrine/DBAL/Statement.php:131 Stack trace: #0 /../applicationFolder/libraries/Doctrine/DBAL/Statement.php(131): PDOStatement->execute(NULL) #1 /../applicationFolder/libraries/Doctrine/ORM/Persisters/BasicEntityPersister.php(239): Doctrine\DBAL\Statement->execute() #2 /../applicationFolder/libraries/Doctrine/ORM/UnitOfWork.php(896): Doctrine\ORM\Persisters\BasicEntityPersister->executeInserts() #3 /../applicationFolder/libraries/Doctrine/ORM/UnitOfWork.php(304): Doctrine\ORM\UnitOfWork->executeInserts(Object(Doctrine\ORM\Mapping\ClassMetadata in /../applicationFolder/libraries/Doctrine/DBAL/Statement.php on line 131

What am I doing wrong?


Is_Unique validation with custom db table - El Forum - 04-06-2012

[eluser]wehappyfew[/eluser]
[quote author="xjermx" date="1333722606"]I am also trying to get unique validation to work.[/quote]

you seem to be off topic


Is_Unique validation with custom db table - El Forum - 04-06-2012

[eluser]xjermx[/eluser]
I thought it seemed exactly on topic.


Is_Unique validation with custom db table - El Forum - 04-07-2012

[eluser]wehappyfew[/eluser]
It seems off topic because I was asking a way to "extend" somehow the is_unique validation so as someone to be able to add a variable inside its arguments to specify a custom table,which would be generated automatically when the new user would be created.

You on the other hand should look more closely the examples of the user guide. The $db variable in the first case is causing the problem.Check if you use it somewhere without having pass it first.
Your syntax seems alright.
The use of the is_unique validation is really straightforward.