Record not found in my view - Cakephp

I’m retrieving data from my Leads Table,

If I try to Add New Lead or Edit Lead works both fine
But when I try to show to the view I’m getting

Record not found in table “leads”

I tried vardump($lead) but no success

LeadsController.php

  public function add()
{
$salePhases = $this->Leads->SalePhases->find('list', ['limit' => 200]);
$campaigns = $this->Leads->Campaigns->find('list', ['limit' => 200]);
$users = $this->Leads->Users->find('list', ['limit' => 200]);
$paidTo = $this->Leads->PaidTo->find('list', ['limit' => 200]);
$secteurs = $this->Leads->Secteurs->find('list', ['limit' => 200]);
$leads = $this->Leads->Leads->find('list', ['limit' => 200]);
$accounts = $this->Leads->Accounts->find('list', ['limit' => 200]);
$sourceProspects = $this->Leads->SourceProspects->find('list', ['limit' => 200]);
$typesLeads = $this->Leads->TypesLeads->find('list', ['limit' => 200]);
$contacts = $this->Leads->Contacts->find('list', ['limit' => 200]);
$this->set(compact('lead', 'salePhases', 'campaigns', 'users', 'paidTo', 'secteurs', 'leads', 
'sourceProspects','accounts','typesLeads','contacts'));
 }

public function view($id = null)
{ 
$lead = $this->Leads->get($id, [
    'contain' => ['SalePhases', 'Campaigns', 'Users', 'PaidTo', 
 'Secteurs', 'Leads', 'SourceProspects', 'Accounts', 'Contacts','TypesLeads']
 ]);
   $this->set('lead', $lead);
 }

LeadsTable

  $this->belongsTo('SalePhases', [
    'foreignKey' => 'sale_phase_id',
    'joinType' => 'INNER'
   ]);
   $this->belongsTo('Campaigns', [
    'foreignKey' => 'campaign_id',
    'joinType' => 'INNER'
    ]);
   $this->belongsTo('Users', [
    'foreignKey' => 'user_id',
    'joinType' => 'INNER'
  ]);
   $this->belongsTo('Accounts', [
     'foreignKey' => 'account_id',
     'joinType' => 'INNER'
   ]);
   $this->belongsTo('Contacts', [
    'foreignKey' => 'contact_id',
    'joinType' => 'INNER'
    ]);
   $this->belongsTo('PaidTo', [
    'foreignKey' => 'paid_to_id',
    'joinType' => 'INNER'
    ]);
   $this->belongsTo('Secteurs', [
    'foreignKey' => 'secteur_id',
    'joinType' => 'INNER'
   ]);
    $this->belongsToMany('Leads', [
     'foreignKey' => 'lead_type_id',
    'joinType' => 'INNER'
   ]);
   $this->hasMany('TypesLeads', [
    'foreignKey' => 'lead_id'
   ]);
    $this->belongsTo('SourceProspects', [
    'foreignKey' => 'source_prospect_id',
     'joinType' => 'INNER'
    ]);
   $this->hasMany('Accounts', [
    'foreignKey' => 'lead_id'
   ]);
   $this->hasMany('Contacts', [
    'foreignKey' => 'lead_id'
   ]);

$rules->add($rules->existsIn(['sale_phase_id'], 'SalePhases'));
$rules->add($rules->existsIn(['campaign_id'], 'Campaigns'));
$rules->add($rules->existsIn(['user_id'], 'Users'));
$rules->add($rules->existsIn(['account_id'], 'Accounts'));
$rules->add($rules->existsIn(['contact_id'], 'Contacts'));
$rules->add($rules->existsIn(['paid_to_id'], 'PaidTo'));
$rules->add($rules->existsIn(['secteur_id'], 'Secteurs'));
$rules->add($rules->existsIn(['lead_type_id'], 'Leads'));
$rules->add($rules->existsIn(['source_prospect_id'], 'SourceProspects'));

view.ctp

           <th scope="row"><?= __('Name Lead') ?></th>
         <td><?= h($lead->name_lead) ?></td>
     </tr>
      <tr>
       <th scope="row"><?= __('Next') ?></th>
      <td><?= h($lead->next) ?></td>
    </tr>
     <tr>
      <th scope="row"><?= __('Description') ?></th>
      <td><?= h($lead->description) ?></td>
     </tr>
     <tr>
    <th scope="row"><?= __('Sale Phase') ?></th>
    <td><?= $lead->has('sale_phase') ? $this->Html->link($lead->sale_phase->id, ['controller' => 
    'SalePhases', 'action' => 'view', $lead->sale_phase->id]) : '' ?></td>
    </tr>
    <tr>
    <th scope="row"><?= __('Campaign') ?></th>
    <td><?= $lead->has('campaign') ? $this->Html->link($lead->campaign->id, ['controller' => 
    'Campaigns', 'action' => 'view', $lead->campaign->id]) : '' ?></td>
    </tr>
    <tr>
    <th scope="row"><?= __('User') ?></th>
     <td><?= $lead->has('user') ? $this->Html->link($lead->user->id, ['controller' => 'Users', 
    'action' => 'view', $lead->user->id]) : '' ?></td>
    </tr>
     <tr>
    <th scope="row"><?= __('Paid To') ?></th>
    <td><?= $lead->has('paid_to') ? $this->Html->link($lead->paid_to->id, ['controller' => 'PaidTo', 
    'action' => 'view', $lead->paid_to->id]) : '' ?></td>
    </tr>
    <tr>
    <th scope="row"><?= __('Secteur') ?></th>
    <td><?= $lead->has('secteur') ? $this->Html->link($lead->secteur->id, ['controller' => 
    'Secteurs', 'action' => 'view', $lead->secteur->id]) : '' ?></td>
    </tr>
    <tr>
    <th scope="row"><?= __('Lead') ?></th>
    <td><?= $lead->has('lead') ? $this->Html->link($lead->lead->id, ['controller' => 'Leads', 
     'action' => 'view', $lead->lead->id]) : '' ?></td>
     </tr>
     <tr>
    <th scope="row"><?= __('Source Prospect') ?></th>
    <td><?= $lead->has('source_prospect') ? $this->Html->link($lead->source_prospect->id, 
     ['controller' => 'SourceProspects', 'action' => 'view', $lead->source_prospect->id]) : '' ?></td>
      </tr>
      <tr>
       <th scope="row"><?= __('Id') ?></th>
      <td><?= $this->Number->format($lead->id) ?></td>
       </tr>
        <tr>
       <th scope="row"><?= __('Forcecast Amount') ?></th>
       <td><?= $this->Number->format($lead->forcecast_amount) ?></td>
       </tr>
       <tr>
       <th scope="row"><?= __('Probability') ?></th>
       <td><?= $this->Number->format($lead->probability) ?></td>
       </tr>
        <tr>
        <th scope="row"><?= __('Account Id') ?></th>
        <td><?= $this->Number->format($lead->account_id) ?></td>
        </tr>
        <tr>
       <th scope="row"><?= __('Contact Id') ?></th>
        <td><?= $this->Number->format($lead->contact_id) ?></td>
       </tr>
        <tr>
       <th scope="row"><?= __('Term') ?></th>
       <td><?= h($lead->term) ?></td>
       </tr>
       <tr>
        <th scope="row"><?= __('Created') ?></th>
       <td><?= h($lead->created) ?></td>
        </tr>
       <tr>
       <th scope="row"><?= __('Modified') ?></th>
       <td><?= h($lead->modified) ?></td>
       </tr>
       </table>
       <div class="related">
      <h4><?= __('Related Accounts') ?></h4>
        <?php if (!empty($lead->accounts)): ?>
       <table cellpadding="0" cellspacing="0">
        <tr>
        <th scope="col"><?= __('Id') ?></th>
        <th scope="col"><?= __('Name Account') ?></th>
        <th scope="col"><?= __('Phone') ?></th>
        <th scope="col"><?= __('Effective') ?></th>
        <th scope="col"><?= __('Other Email') ?></th>
        <th scope="col"><?= __('Email Opt Out') ?></th>
        <th scope="col"><?= __('Web Site') ?></th>
        <th scope="col"><?= __('Fax') ?></th>
        <th scope="col"><?= __('Other Phone') ?></th>
        <th scope="col"><?= __('Email') ?></th>
        <th scope="col"><?= __('Owner') ?></th>
        <th scope="col"><?= __('Code Ape') ?></th>
        <th scope="col"><?= __('Annual Revenue') ?></th>
        <th scope="col"><?= __('Notify Owner') ?></th>
        <th scope="col"><?= __('Billing Addres') ?></th>
        <th scope="col"><?= __('Delivery Address') ?></th>
        <th scope="col"><?= __('Description') ?></th>
        <th scope="col"><?= __('Comments') ?></th>
        <th scope="col"><?= __('Created') ?></th>
        <th scope="col"><?= __('Modified') ?></th>
        <th scope="col"><?= __('Account Id') ?></th>
        <th scope="col"><?= __('Note Id') ?></th>
        <th scope="col"><?= __('User Id') ?></th>
        <th scope="col"><?= __('Lead Id') ?></th>
        <th scope="col"><?= __('Contact Id') ?></th>
        <th scope="col"><?= __('Secteur Id') ?></th>
         <th scope="col" class="actions"><?= __('Actions') ?></th>
       </tr>
      <?php foreach ($lead->accounts as $accounts): ?>
        <tr>
         <td><?= h($accounts->id) ?></td>
          <td><?= h($accounts->name_account) ?></td>
           <td><?= h($accounts->phone) ?></td>
          <td><?= h($accounts->effective) ?></td>
          <td><?= h($accounts->other_email) ?></td>
          <td><?= h($accounts->email_opt_out) ?></td>
         <td><?= h($accounts->web_site) ?></td>
          <td><?= h($accounts->fax) ?></td>
         <td><?= h($accounts->other_phone) ?></td>
         <td><?= h($accounts->email) ?></td>
         <td><?= h($accounts->owner) ?></td>
         <td><?= h($accounts->code_ape) ?></td>
          <td><?= h($accounts->annual_revenue) ?></td>
         <td><?= h($accounts->notify_owner) ?></td>
         <td><?= h($accounts->billing_addres) ?></td>
         <td><?= h($accounts->delivery_address) ?></td>
         <td><?= h($accounts->description) ?></td>
         <td><?= h($accounts->comments) ?></td>
         <td><?= h($accounts->created) ?></td>
         <td><?= h($accounts->modified) ?></td>
          <td><?= h($accounts->account_id) ?></td>
          <td><?= h($accounts->note_id) ?></td>
         <td><?= h($accounts->user_id) ?></td>
          <td><?= h($accounts->lead_id) ?></td>
          <td><?= h($accounts->contact_id) ?></td>
          <td><?= h($accounts->secteur_id) ?></td>
          <td class="actions">
            <?= $this->Html->link(__('View'), ['controller' => 'Accounts', 'action' => 'view', $accounts- 
             >id]) ?>
            <?= $this->Html->link(__('Edit'), ['controller' => 'Accounts', 'action' => 'edit', $accounts- 
           >id]) ?>
            <?= $this->Form->postLink(__('Delete'), ['controller' => 'Accounts', 'action' => 'delete', 
            $accounts->id], ['confirm' => __('Are you sure you want to delete # {0}?', $accounts- 
           >id)]) ?>
         </td>
      </tr>
      <?php endforeach; ?>
      </table>
     <?php endif; ?>
     </div>
     <div class="related">
      <h4><?= __('Related Contacts') ?></h4>
       <?php if (!empty($lead->contacts)): ?>
      <table cellpadding="0" cellspacing="0">
        <tr>
        <th scope="col"><?= __('Id') ?></th>
        <th scope="col"><?= __('Name') ?></th>
        <th scope="col"><?= __('Phone') ?></th>
        <th scope="col"><?= __('Mobile') ?></th>
        <th scope="col"><?= __('Other Phone') ?></th>
        <th scope="col"><?= __('Fax') ?></th>
        <th scope="col"><?= __('Do Not Call') ?></th>
        <th scope="col"><?= __('Interested In') ?></th>
        <th scope="col"><?= __('Notify Owner') ?></th>
        <th scope="col"><?= __('Referent') ?></th>
        <th scope="col"><?= __('First Name') ?></th>
        <th scope="col"><?= __('Entreprise') ?></th>
        <th scope="col"><?= __('Fonction') ?></th>
        <th scope="col"><?= __('Email') ?></th>
        <th scope="col"><?= __('Assistant') ?></th>
        <th scope="col"><?= __('Email Opt Out') ?></th>
        <th scope="col"><?= __('Client') ?></th>
        <th scope="col"><?= __('Service Portal Access') ?></th>
        <th scope="col"><?= __('Support Start Date') ?></th>
        <th scope="col"><?= __('End Date Support') ?></th>
        <th scope="col"><?= __('Wilaya') ?></th>
        <th scope="col"><?= __('City') ?></th>
        <th scope="col"><?= __('Created') ?></th>
        <th scope="col"><?= __('Modified') ?></th>
        <th scope="col"><?= __('User Id') ?></th>
        <th scope="col"><?= __('Contact Type Id') ?></th>
        <th scope="col"><?= __('Lead Id') ?></th>
        <th scope="col"><?= __('Source Prospect Id') ?></th>
        <th scope="col"><?= __('Account Id') ?></th>
        <th scope="col"><?= __('Statut Id') ?></th>
        <th scope="col"><?= __('Secteur Id') ?></th>
        <th scope="col"><?= __('Product Id') ?></th>
        <th scope="col" class="actions"><?= __('Actions') ?></th>
        </tr>
        <?php foreach ($lead->contacts as $contacts): ?>
        <tr>
        <td><?= h($contacts->id) ?></td>
        <td><?= h($contacts->name) ?></td>
        <td><?= h($contacts->phone) ?></td>
        <td><?= h($contacts->mobile) ?></td>
        <td><?= h($contacts->other_phone) ?></td>
        <td><?= h($contacts->fax) ?></td>
        <td><?= h($contacts->do_not_call) ?></td>
        <td><?= h($contacts->interested_in) ?></td>
        <td><?= h($contacts->notify_owner) ?></td>
        <td><?= h($contacts->referent) ?></td>
        <td><?= h($contacts->first_name) ?></td>
        <td><?= h($contacts->entreprise) ?></td>
        <td><?= h($contacts->fonction) ?></td>
        <td><?= h($contacts->email) ?></td>
        <td><?= h($contacts->assistant) ?></td>
        <td><?= h($contacts->email_opt_out) ?></td>
        <td><?= h($contacts->client) ?></td>
        <td><?= h($contacts->service_portal_access) ?></td>
        <td><?= h($contacts->support_start_date) ?></td>
        <td><?= h($contacts->end_date_support) ?></td>
        <td><?= h($contacts->wilaya) ?></td>
        <td><?= h($contacts->city) ?></td>
        <td><?= h($contacts->created) ?></td>
        <td><?= h($contacts->modified) ?></td>
        <td><?= h($contacts->user_id) ?></td>
        <td><?= h($contacts->contact_type_id) ?></td>
        <td><?= h($contacts->lead_id) ?></td>
        <td><?= h($contacts->source_prospect_id) ?></td>
        <td><?= h($contacts->account_id) ?></td>
        <td><?= h($contacts->statut_id) ?></td>
        <td><?= h($contacts->secteur_id) ?></td>
        <td><?= h($contacts->product_id) ?></td>
        <td class="actions">
            <?= $this->Html->link(__('View'), ['controller' => 'Contacts', 'action' => 'view', $contacts- 
          >id]) ?>
             <?= $this->Html->link(__('Edit'), ['controller' => 'Contacts', 'action' => 'edit', $contacts- 
           >id]) ?>
            <?= $this->Form->postLink(__('Delete'), ['controller' => 'Contacts', 'action' => 'delete', 
         $contacts->id], ['confirm' => __('Are you sure you want to delete # {0}?', $contacts->id)]) ? 
        >
         </td>
        </tr>
        <?php endforeach; ?>
     </table>
      <?php endif; ?>
     </div>

Edit :
Debugging SQL LOG

  SELECT 
`Leads`.`id` AS `Leads__id`, 
`Leads`.`name_lead` AS `Leads__name_lead`, 
`Leads`.`forcecast_amount` AS `Leads__forcecast_amount`, 
`Leads`.`term` AS `Leads__term`, 
`Leads`.`next` AS `Leads__next`, 
`Leads`.`probability` AS `Leads__probability`, 
`Leads`.`description` AS `Leads__description`, 
`Leads`.`created` AS `Leads__created`, 
`Leads`.`modified` AS `Leads__modified`, 
`Leads`.`sale_phase_id` AS `Leads__sale_phase_id`, 
`Leads`.`campaign_id` AS `Leads__campaign_id`, 
`Leads`.`user_id` AS `Leads__user_id`, 
`Leads`.`account_id` AS `Leads__account_id`, 
`Leads`.`contact_id` AS `Leads__contact_id`, 
`Leads`.`paid_to_id` AS `Leads__paid_to_id`, 
`Leads`.`secteur_id` AS `Leads__secteur_id`, 
`Leads`.`lead_type_id` AS `Leads__lead_type_id`, 
`Leads`.`source_prospect_id` AS `Leads__source_prospect_id`, 
`SalePhases`.`id` AS `SalePhases__id`, 
`SalePhases`.`name_phase` AS `SalePhases__name_phase`, 
`SalePhases`.`user_id` AS `SalePhases__user_id`, 
`Campaigns`.`id` AS `Campaigns__id`, 
`Campaigns`.`name_campaign` AS `Campaigns__name_campaign`, 
`Campaigns`.`user_id` AS `Campaigns__user_id`, 
`Users`.`id` AS `Users__id`, 
`Users`.`full_name` AS `Users__full_name`, 
`Users`.`email` AS `Users__email`, 
`Users`.`password` AS `Users__password`, 
`Users`.`created` AS `Users__created`, 
`Users`.`modified` AS `Users__modified`, 
`Users`.`username` AS `Users__username`, 
`Users`.`role` AS `Users__role`, 
`Users`.`passkey` AS `Users__passkey`, 
`Users`.`timeout` AS `Users__timeout`, 
`Users`.`photo` AS `Users__photo`, 
`Users`.`photo_dir` AS `Users__photo_dir`, 
`PaidTo`.`id` AS `PaidTo__id`, 
`PaidTo`.`percentage` AS `PaidTo__percentage`, 
`PaidTo`.`user_id` AS `PaidTo__user_id`, 
`Secteurs`.`id` AS `Secteurs__id`, 
`Secteurs`.`name_secteur` AS `Secteurs__name_secteur`, 
`Secteurs`.`user_id` AS `Secteurs__user_id`, 
`SourceProspects`.`id` AS `SourceProspects__id`, 
`SourceProspects`.`name_prospect` AS `SourceProspects__name_prospect`, 
`SourceProspects`.`user_id` AS `SourceProspects__user_id` 
FROM 
`leads` `Leads` 
INNER JOIN `sale_phases` `SalePhases` ON `SalePhases`.`id` = 
(`Leads`.`sale_phase_id`) 
INNER JOIN `campaigns` `Campaigns` ON `Campaigns`.`id` = 
(`Leads`.`campaign_id`) 
INNER JOIN `users` `Users` ON `Users`.`id` = (`Leads`.`user_id`) 
INNER JOIN `paid_to` `PaidTo` ON `PaidTo`.`id` = (`Leads`.`paid_to_id`) 
INNER JOIN `secteurs` `Secteurs` ON `Secteurs`.`id` = 
(`Leads`.`secteur_id`) 
INNER JOIN `source_prospects` `SourceProspects` ON `SourceProspects`.`id` 
= 
(`Leads`.`source_prospect_id`) 
WHERE 
`Leads`.`id` = 5 
LIMIT 
1

Hi,
open your Leads table in programm like phpAdmin and check record ID=5, just in case :slight_smile:
All your tables has INNER JOIN ([‘SalePhases’, ‘Campaigns’, ‘Users’, ‘PaidTo’,
‘Secteurs’, ‘Leads’, ‘SourceProspects’, ‘Accounts’, ‘Contacts’,‘TypesLeads’]) It’s mean you need to have all relaited records in these tables. In phpAdmin you can run SQL and exclude each time one JOIN , when you find Leads.id = 5 check the last excluded JOIN(table), if you haven’t many records you can find missing data by eyes.

BR.