$user = new User(); if ($user->hasField('email')) { echo "Email field exists"; } else { echo "Email field does not exist"; }
$post = new Post(); $fields = ['title', 'content', 'author_id', 'created_at']; foreach ($fields as $field) { if ($post->hasField($field)) { echo "$field field exists"; } else { echo "$field field does not exist"; } }In this example, we create a new instance of the Post model and loop through an array of field names. We check if each field exists in the model and print a message accordingly. The hasField() function is part of the CakePHP framework, specifically in the Model class from the CakePHP database package library.