oneofakind
oneofakind

Reputation: 562

Symfony How to implement Models

I am using Symfony 1.4 and already created schema.yml, 3 of them, and created the database.yml with multiple connection.

And executed the build --model of symfony and created classes one of them is "Schedule" which came from the schema having columns: id, name, description....

How do use that class to use its method or function, the setter and getters? Books says that there are setters and getter if a model was generated.

How do I retrieve data with filters?

Upvotes: 0

Views: 78

Answers (1)

j0k
j0k

Reputation: 22756

Your question is a bit vague. But from your model you can do:

Create an entry:

$schedule = new Schedule();
$schedule->setName('foo');
$schedule->setDescription('bar');
$schedule->save();

Find all entries

$schedules = Doctrine_Core::getTable('Schedule')->findAll();

Retrieve one item (if we assume that one Schedule exists with id 1)

$schedule = Doctrine_Core::getTable('Schedule')->find(1);
$schedule = Doctrine_Core::getTable('Schedule')->findOneByName('foo');

Access field inside your model

$name        = $schedule->getName();
$description = $schedule->getDescription();

Edit:

Custom getter, in ScheduleTable.class.php:

public function findByStatusAndRangeTime($status, $start, $end)
{
  $q = $this->createQuery('s');
  $q->where('s.status = ? AND s.time_start < ? AND s.time_end > ?', array($status, $start, $end));

  return $q->execute();
}

Then, you can call it using:

$schedule = Doctrine_Core::getTable('Schedule')->findByStatusAndRangeTime('pending', '1:00', '5:00');

Upvotes: 1

Related Questions