Welcome Guest, Not a member yet? Register   Sign In
Is_Unique validation with custom db table

Hello to all,

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

function add_client($form_inputs)
  //create the table name
  $username = $this->session->userdata('username');
  $table_name = $username . "_clients";
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:

$field_rules =array
                     'field'   => 'client_name',
                     'label'   => '-Client\'s name-',
                     'rules'   =>
----------------> is_unique[{$username}_clients.client_name]|
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.

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?

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.

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:

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.

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.


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:
$this->form_validation->set_rules('email', 'E-mail',

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:

$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?

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

you seem to be off topic

I thought it seemed exactly on topic.

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.

Theme © iAndrew 2016 - Forum software by © MyBB