XF 2.2 Edit link opens Add New instead of Edit page

FoxSecrets

Active member
My edit link is not working as expected, it's opening the "add new" instead of "edit" page. Am I missing something?

The link is like: http://localhost:8000/admin.php?my-page/5/edit

ROUTE
Code:
Route prefix: my-page/
Route format: :int<product_id>

TEMPLATE INDEX
Code:
<xf:foreach loop="$xf.app.db.fetchAll('SELECT * FROM xf_my_page')" key="$dataId" value="$item">
     ...
    <xf:cell><a href="{{ link('my-page/edit', $item) }}">Edit</a></xf:cell>
</xf:foreach>

TEMPLATE NEW/EDIT (my_page_edit)
Code:
<xf:form action="{{ link('my-page/save', $item) }}" class="block" rowclass="formRow--input" ajax="1">
    ...
    <xf:hiddenval name="product_id" id="product_id" required="true">{{ $item.product_id }}</xf:hiddenval>
    <xf:textboxrow name="product_name" value="{{ $item.product_name }}" label="Product" />
    <xf:submitrow submit="Save" fa="fa-save" icon="" />
</xf:form>

PHP:
Code:
public function actionNew()
    {
        $data = $this->em()->create('My\Addon:MyClass');
        return $this->dataAddEdit($data);
    }

    public function actionEdit(ParameterBag $params)
    {
        $data = $this->assertProductExists($params->product_id);
        return $this->dataAddEdit($data);
    }

    protected function dataAddEdit(\My\Addon\Entity\Class $data)
    {
        $viewParams = [
            'data' => $data
        ];
        return $this->view('My\Addon:MyClass\Edit', 'my_page_edit', $viewParams);
    }

    /**
     * @param $id
     * @param null $with
     * @param null $phraseKey
     * @return \My\Addon\Entity\Class
     * @throws \XF\Mvc\Reply\Exception
     */
    protected function assertProductExists($id, $with = null, $phraseKey = null)
    {
        return $this->assertRecordExists('My\Addon:MyClass', $id, $with, $phraseKey);
    }
 
Last edited:
It looks fine at a glance. If you {{ dump($data) }} in the my_page_edit template does it dump the corresponding entity?
 
Yeah, I forgot this inspection tool. I gotta use it more often, very useful.
My data was null then I noticed a typo error in

Code:
$viewParams = [
            'data' => $data
        ];

The 'data' was originally written "products", plural instead of singular (that was in use). Thank you Jeremy.
 
Top Bottom