Saving additional string columns as null or empty string? #291
-
For example, I would like to add <?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class AdditionalUserFields extends Migration
{
public function up(): void
{
$this->forge->addColumn('users', [
'first_name' => [
'type' => 'varchar',
'constraint' => 255,
'null' => true,
'after' => 'username',
],
'last_name' => [
'type' => 'varchar',
'constraint' => 255,
'null' => true,
'after' => 'first_name',
],
]);
}
public function down(): void
{
$this->forge->dropColumn('users', ['first_name', 'last_name']);
}
} I'll add input fields to save this columns, but If no data is provided then columns by default are not saved as null but as empty string. What is now better to do with varchars, save them as null or empty string? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Is a way with using https://codeigniter4.github.io/userguide/models/entities.html#filling-properties-quickly a proper way to save post data ? <?php
$data = $this->request->getPost();
$user = new \App\Entities\User($data);
$userModel->save($user); If I want for example to save first_name as null then I need to somehow check if first_name was filled and if not the to do something like? if ($user->first_name === '') {
$user->first_name = null;
} and then $userModel->save($user); Is this a correct way ? or do we have some function how to save empty inputs as null? |
Beta Was this translation helpful? Give feedback.
-
Do you need to distinguish between null and empty string? But there is RDBMS that does not distinguish between null and empty string. |
Beta Was this translation helpful? Give feedback.
Do you need to distinguish between null and empty string?
If so, you need to use null.
If not, you don't need null.
But there is RDBMS that does not distinguish between null and empty string.