JoinClause.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Illuminate\Database\Query;
  3. use Closure;
  4. class JoinClause extends Builder
  5. {
  6. /**
  7. * The type of join being performed.
  8. *
  9. * @var string
  10. */
  11. public $type;
  12. /**
  13. * The table the join clause is joining to.
  14. *
  15. * @var string
  16. */
  17. public $table;
  18. /**
  19. * The parent query builder instance.
  20. *
  21. * @var \Illuminate\Database\Query\Builder
  22. */
  23. private $parentQuery;
  24. /**
  25. * Create a new join clause instance.
  26. *
  27. * @param \Illuminate\Database\Query\Builder $parentQuery
  28. * @param string $type
  29. * @param string $table
  30. * @return void
  31. */
  32. public function __construct(Builder $parentQuery, $type, $table)
  33. {
  34. $this->type = $type;
  35. $this->table = $table;
  36. $this->parentQuery = $parentQuery;
  37. parent::__construct(
  38. $parentQuery->getConnection(), $parentQuery->getGrammar(), $parentQuery->getProcessor()
  39. );
  40. }
  41. /**
  42. * Add an "on" clause to the join.
  43. *
  44. * On clauses can be chained, e.g.
  45. *
  46. * $join->on('contacts.user_id', '=', 'users.id')
  47. * ->on('contacts.info_id', '=', 'info.id')
  48. *
  49. * will produce the following SQL:
  50. *
  51. * on `contacts`.`user_id` = `users`.`id` and `contacts`.`info_id` = `info`.`id`
  52. *
  53. * @param \Closure|string $first
  54. * @param string|null $operator
  55. * @param string|null $second
  56. * @param string $boolean
  57. * @return $this
  58. *
  59. * @throws \InvalidArgumentException
  60. */
  61. public function on($first, $operator = null, $second = null, $boolean = 'and')
  62. {
  63. if ($first instanceof Closure) {
  64. return $this->whereNested($first, $boolean);
  65. }
  66. return $this->whereColumn($first, $operator, $second, $boolean);
  67. }
  68. /**
  69. * Add an "or on" clause to the join.
  70. *
  71. * @param \Closure|string $first
  72. * @param string|null $operator
  73. * @param string|null $second
  74. * @return \Illuminate\Database\Query\JoinClause
  75. */
  76. public function orOn($first, $operator = null, $second = null)
  77. {
  78. return $this->on($first, $operator, $second, 'or');
  79. }
  80. /**
  81. * Get a new instance of the join clause builder.
  82. *
  83. * @return \Illuminate\Database\Query\JoinClause
  84. */
  85. public function newQuery()
  86. {
  87. return new static($this->parentQuery, $this->type, $this->table);
  88. }
  89. /**
  90. * Create a new query instance for sub-query.
  91. *
  92. * @return \Illuminate\Database\Query\Builder
  93. */
  94. protected function forSubQuery()
  95. {
  96. return $this->parentQuery->newQuery();
  97. }
  98. }