php - Ensure field in database record is required -


how can ensure field in table required?

for example: have posts table , users table. when post saved requires user_id field.

where if did this:

$post = new post; $posttext = "test post one"; $post->text = $posttext; $post->save(); 

i error because there no user_id field. instead need this:

$post = new post; $posttext = "test post one"; $post->text = $posttext; $post->user_id = 1; $post->save(); 

is done by

  • overriding save() method of post class?
  • hooking model events?

there several ways this, i'm giving basic using if statement. since fields predefined, can check if fields has corresponding value. can use:

if($post->text == "" || $post->text == null){     something... } 

or yes can touch model , check there.

thanks


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -