0

I'm working on one module based on sfGuard and I need to allow users to edit or delete records. This is how I get the users:

$id_empresa = $this->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
$this->users = Doctrine_Core::getTable('sfGuardUser')->createQuery('u')->leftJoin('u.SfGuardUserProfile p')->where('idempresa = ?', $id_empresa)->execute();

But then in the view while I'm iterating I don't know how to access to user_id value. I checked BasesfGuardUser but no method for get the Id exists, any advice or help?

Added the code for the view This is the code for the iteration:

<?php foreach ($users as $user): ?>
    <tr>
       <td><?php echo $user->getFirstName() ?></td>
       <td><?php echo $user->getLastName() ?></td>
       <td><?php echo $user->getEmailAddress() ?></td>
       <td><?php echo $user->getUsername() ?></td>
       <td><?php echo $user->getIsActive() ?></td>
       <td>
          <a href="<?php echo url_for('guard/users/' . $user->get('id') . '/edit') ?>" class="btn btn-success btn-mini">Editar</a>
          <a href="<?php echo url_for('guard/users/' . $user->get('id')) ?>" class="btn btn-danger btn-mini">Eliminar</a>
       </td>
    </tr>
<?php endforeach; ?>
Reynier
  • 2,420
  • 11
  • 51
  • 91
  • `foreach ($users as $user) var_dump($user->get('id'))` – 1ed Jun 17 '13 at 18:41
  • @1ed, nope, doesn't work – Reynier Jun 17 '13 at 18:49
  • that should work (maybe `idempresa` is the name of the id coloumn?), but try `$user->getPrimaryKey()` then... and if you want to generate URLs you should use the route name and the object as parameter like `url_for('sf_gaurd_user_edit', $user);`. `php symfony app:routes backend` to see available routes – 1ed Jun 17 '13 at 19:09
  • @1ed ups apologies man, both works I mean the first solution you leave and the second one based on `getPrimaryKey()`, the problem is here: `Editar` why the value isn't append to the URL? – Reynier Jun 17 '13 at 21:46
  • No worries :)... I added an answer where I try to explain the problem. – 1ed Jun 17 '13 at 22:36

1 Answers1

2

The problem is the 'guard/users/' . $user->get('id') . '/edit' part in url_for. Maybe it's because usuarios/edit/1 is not a valid parameter for url_for so it's parsed as module/action and the /1 part is ignored, but I'm not sure.

IIRC url_for actually has 4 valid form:

  • url_for('module/action', array('param1' => 'value1'), true /* absolute */)
  • url_for('@route_name?param1=value1', true /* absolute */)
  • url_for('route_name', array('param1' => 'value1'), true /* absolute */)
  • this is for object routes only: url_for('route_name', $object, true /* absolute */) or url_for('route_name', array('sf_subject' => $object, 'extra_param' => 'value'), true /* absolute */)

If you have an object route (e.g. sfDoctrineRoute) you should use the last one, if you have a non-object route with parameters you should use the 3rd and if you have a route with no parameter you should use the 2nd.

1ed
  • 3,668
  • 15
  • 25