0

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?

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

3 Comments

this line "Doctrine_Core::getTable('Schedule')->find(1)" how does the system know "1" is to "id" or not any columns? how can I specify the filters?
Because id is the default primary key in Doctrine. So find() method use the PK to search on the table. What do you mean by filters?
Like if for example Schedule has a column of status, time-start and time-end, I would want to filter it to only completed status in a certain time-star up to a certain time end. Like only status = pending, time-start < 1:00 and time-end > 5:00..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.